【问题标题】:Redirect to action with an attribute in ASP.NET MVC使用 ASP.NET MVC 中的属性重定向到操作
【发布时间】:2019-09-26 05:43:44
【问题描述】:

我正在尝试使用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 });
  • 感谢@Rahul,成功了!如果您可以发布您的答案,我会接受。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    相关资源
    最近更新 更多