c# - 使用 ASP.NET MVC 中的属性重定向到操作
问题描述
我试图从一个控制器方法重定向到同一个控制器中的另一个方法,使用RedirectToAction
. 但是,到目前为止它还没有工作。
我能想到的一个原因是我在目标方法上的属性路由。
public class PostController : Controller
{
private static readonly PostRepository postRepo = new PostRepository();
[Route("post/{id}")]
public ActionResult GetPost(int id)
{
Post post = postRepo.GetPostById(id);
return View("Index", post);
}
[Route("post/{id}/comment")]
[HttpPost]
public void Comment(int id, string text)
{
Post post = postRepo.GetPostById(id);
var comment = GetComment(text);
postRepo.AddComment(comment);
RedirectToAction("post", new { id }); // Didn't work
RedirectToAction("GetPost", new { id }); // Didn't work
}
}
我在这里想念什么?
解决方案
关于您的情况,您需要返回结果:
return RedirectToAction("GetPost", new { id });
推荐阅读
- c# - 无法在 C# 中使用 PdfMetamorphosis 捕获异常
- vue.js - 变量类名的语法(或在 v-if 中)
- javascript - Angular 6+ 应用程序的运行时配置
- python - pip install py2neo 失败
- ruby-on-rails - Rails 没有从关联中获得价值
- angular - 如果用户已经登录,则 Angular 登录组件重定向
- sql - 我需要在 oracle 中创建一个过程并且必须实现以下逻辑
- javascript - 我在哪里可以获得将法定货币金额转换为加密货币价值的加密 API
- php - 在 Woocommerce 中从某个产品类别购买产品时重定向
- javascript - 在 Javascript eval 函数中调用使用“import”导入的模块