简体   繁体   English

使用 ASP.NET MVC 中的属性重定向到操作

[英]Redirect to action with an attribute in ASP.NET MVC

I am trying to redirect from a controller method to another method in the same controller, by using RedirectToAction .我正在尝试使用RedirectToAction从 controller 方法重定向到同一 controller 中的另一种方法。 However, it's not working so far.但是,到目前为止它还没有工作。

One reason I can think of is the attribute route I have on my target method.我能想到的一个原因是我在目标方法上的属性路由。

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
    }
}

What am I missing here?我在这里想念什么?

Regarding your case, you need to return the result:关于您的情况,您需要返回结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM