【问题标题】:AspCore Multiple Post Actions with Same Action Name具有相同操作名称的 Asp Core 多个发布操作
【发布时间】:2017-09-01 02:54:31
【问题描述】:

我的结帐页面有多种付款方式。每个方法都有自己的局部视图,其中包含自己的模型。我试图在每种不同的方法上保持 url 相同,所以如果出现错误,url 不会改变。有没有办法做到这一点?感谢您的帮助,我已经考虑了一段时间了。

结帐模型

 public class CheckoutForm
{

    public Method1Form method1Form { get; set; }
    public Method2Form method2Form { get; set; }
    public Method3Form method3Form { get; set; }
}

结帐控制器

[HttpGet]
[Route("checkout/{guid}")]
public IActionResult Checkout([FromRoute] String guid)
{
    ....
    return View(model);
}
[HttpPost]
[Route("checkout/{guid}")]
public IActionResult Checkout([FromRoute] String guid, Method1 model)
{
    ....
    //Some Error Condition Triggered
    return View(checkoutmodel);
}
[HttpPost]
[Route("checkout/{guid}")]
public IActionResult Checkout([FromRoute] String guid, Method2 model)
{
    ....
    //Some Error Condition Triggered
    return View(checkoutmodel);
}
[HttpPost]
[Route("checkout/{guid}")]
public IActionResult Checkout([FromRoute] String guid, Method3 model)
{
    ....
    //Some Error Condition Triggered
    return View(checkoutmodel);
}

没有答案的类似问题https://stackoverflow.com/questions/42644136

【问题讨论】:

    标签: asp.net-core


    【解决方案1】:

    你不能。 Route Engine 无法区分这 3 种 post 方法。

    您可以在 URL 的末尾附加一些内容以使它们不同。

    [HttpPost]
    [Route("checkout/{guid}/paypal")]
    public IActionResult Checkout([FromRoute] String guid, Method1 model)
    {
        ....
    }
    
    [HttpPost]
    [Route("checkout/{guid}/authorizenet")]
    public IActionResult Checkout([FromRoute] String guid, Method2 model)
    {
        ....
    }
    

    【讨论】:

    • 感谢我需要的缺失部分。我不希望 url 改变,以防用户在收到错误后重新加载页面,因为 checkout/Method1/{guid} 返回 404。我一直挂断始终将变量放在最后。现在我可以在 Get 上执行 [Route("checkout/{guid}/{method?}")] 并且在刷新后仍然允许页面存在。
    猜你喜欢
    • 2016-05-17
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2017-05-24
    相关资源
    最近更新 更多