首页 > 解决方案 > 使用 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
    }
}

我在这里想念什么?

标签: c#asp.net-mvcasp.net-mvc-4

解决方案


关于您的情况,您需要返回结果:

return RedirectToAction("GetPost", new { id });

推荐阅读