1

I have a checkout page that has multiple methods for payment. Each method has its own partial view containing its own model. I am trying to get keep the url the same on each of the different methods so if there is an error the url doesn't change. Is there a way to achieve this? Thank you for any help, I have been mulling over this for a while now.

CheckOut Model

 public class CheckoutForm
{

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

CheckOut Controller

[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);
}

Similar Question without an answer https://stackoverflow.com/questions/42644136

Lee Reitz
  • 105
  • 10

1 Answers1

1

You cannot. There is no way for Route Engine to differentiate those 3 post methods.

You could append something at the end to the URL to make them different.

[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)
{
    ....
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • Thanks for the missing piece I was needing. I didnt want the url to change in case the user reloads the page after getting an error since checkout/Method1/{guid} returns a 404. i got hung up on always putting the variable at the end. Now I am able to do [Route("checkout/{guid}/{method?}")] on the Get and still allow the page to live after a refresh. – Lee Reitz Aug 31 '17 at 19:04