【问题标题】:Redirect from POST action to POST action从 POST 动作重定向到 POST 动作
【发布时间】:2017-10-28 21:24:52
【问题描述】:

我有 2 个 POST 操作,每个都想从一个重定向到另一个:

[HttpPost]
public IActionResult Foo()
{
    bool isBar = handleFoo();
    if (isBar) return RedirectToAction("Bar");
    else return View();
}


[HttpPost]
public IActionResult Bar()
{
    bool isFoo = handleBar();
    if (isFoo) return RedirectToAction("Foo");
    else return View();
}

假设用户提交表单并运行Bar方法,如果布尔表达式isFoo为真,则返回Foo方法及其视图,否则返回当前Bar视图。

目前,我的代码无法为操作返回正确的视图,我该怎么做?

【问题讨论】:

  • RedirectToAction 是 GET,而不是 POST
  • 将 foo 设为 httpGet
  • @Saurabh 这两个都是POST方法。
  • @MiP 如果您不能将其更改为 httpGet 而不是将其删除并使用 [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)] ,则需要详细说明您为什么希望发布方法发布方法重定向的逻辑

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


【解决方案1】:

请在您的情况下也指定视图名称:

[HttpPost]
public IActionResult Foo()
{
    bool isBar = handleFoo();
    if (isBar) return Bar();
    else return View("Foo");
}


[HttpPost]
public IActionResult Bar()
{
    bool isFoo = handleBar();
    if (isFoo) return Foo();
    else return View("Bar");
}

【讨论】:

    【解决方案2】:

    RedirectToAction 使用的是 GEt 而不是 POST,所以我假设这两个动作都在同一个控制器中,这样你就可以调用另一个动作作为调用这样的方法

    [HttpPost]
    public IActionResult Foo()
    {
        bool isBar = handleFoo();
        if (isBar) return Bar();
        else return View();
    }
    
    
    [HttpPost]
    public IActionResult Bar()
    {
        bool isFoo = handleBar();
        if (isFoo) return Foo();
        else return View();
    }
    

    【讨论】:

    • 这就是我所做的,但它返回了错误的视图,即Foo() 返回了Bar(),但仍然获得了 Foo 的视图。
    • handleFoo 和 HandleBar 方法是什么?
    • 在当前的例子中没关系,你可以认为它返回Random.NextDouble() > 0.5
    • 你调试了吗?
    • Mip 是正确的 Foo 总是只返回它自己的视图。
    【解决方案3】:

    希望我能解决你的问题,代码应该是这样的。

    [HttpGet]
    public IActionResult Foo()
    {
        bool isBar = handleFoo();
        if (isBar) return RedirectToAction("Bar");
        else return View();
    }
    
    
    [HttpPost]
    public IActionResult Bar()
    {
        bool isFoo = handleBar();
        if (isFoo) return RedirectToAction("Foo");
        else return View();
    }
    

    注意:请将 foo() 方法的 action 属性设为 [HttpGet]。

    因为 RedirectToAction() 只支持 get 请求,也就是说,它就像是从浏览器发出一个新的请求。

    对于 Bar() 方法,您可以将 actionattribute 保留为 [httppost] ,因为它是从表单调用的。

    希望以上信息对解决您的问题有用,请告诉我您的想法或反馈

    谢谢

    卡提克

    【讨论】:

    • 这两个都是POST方法。
    • 然后试试下面的链接,看看它是否有效:stackoverflow.com/a/3740110/3397630
    • 这可行,但我担心 ASP.NET 会选择 GET 并且我不想在 URL 中公开紧急查询,而不是我现在正在寻找的内容。顺便说一句,.NET Core 中的正确属性应该是 [AcceptVerbs("Get", "Post")]
    • [HttpPost] 是 [AcceptVerbs(HttpVerbs.Post)] 的简写。唯一的区别是你不能在同一个动作上同时使用 [HttpGet, HttpPost] (和类似的)。如果你想让一个动作同时响应 GET 和 POST,你必须使用 [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] 来源/取自:stackoverflow.com/a/16034262/3397630希望对您有所帮助,谢谢
    • 我认为HttpVerbs 尚未在 .NET Core 1.1 中得到支持,因此我们必须使用字符串数组。关键是我不能让我的方法接受GET,所以不接受[AcceptVerbs("Get", "Post")]
    猜你喜欢
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    相关资源
    最近更新 更多