2

I have 2 POST actions, each one wants to redirect from one to another:

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

Assumedly, the user submits a form and runs the Bar method, if a boolean expression isFoo is true, then return the Foo method with its view, else return the current Bar view.

Currently, my code cannot return the correct View for the action, how can I do it?

MiP
  • 5,846
  • 3
  • 26
  • 41

3 Answers3

2

Please specify the View name also in your case:

[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");
}
Prateek Pandey
  • 833
  • 6
  • 19
0

RedirectToAction is using GEt not POST, so I would assume that the both actions in same controller so you can call the other action as calling a method like this

[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();
}
Feras Salim
  • 438
  • 7
  • 33
0

Hope i got a solution for your problem , and the code should look like this.

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

Note : Kindly make the action attribute for foo() method as [HttpGet].

Because RedirectToAction() supports only the get request, ie.it is like give a new request from the browser.

And for the Bar() method you can retain actionattribute as [httppost] , since it is calling from the form.

Hope the above information will be useful to solve your problem, kindly let me know your thoughts or feedbacks

Thanks

Karthik

Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12
  • Both of them are POST methods. – MiP May 29 '17 at 10:55
  • Then Try the below link and see whether it works :https://stackoverflow.com/a/3740110/3397630 – Karthik Elumalai May 29 '17 at 16:16
  • This could work, but I'm afraid ASP.NET would choose `GET` and I don't want to expose urgly queries in the URL, not what I'm looking for at the momment. btw the correct attribute in .NET Core should be `[AcceptVerbs("Get", "Post")]` – MiP May 29 '17 at 16:19
  • [HttpPost] is shorthand for [AcceptVerbs(HttpVerbs.Post)]. The only difference is that you can't use [HttpGet, HttpPost] (and similar) together on the same action. If you want an action to respond to both GETs and POSTs, you must use [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] Source/Taken From: https://stackoverflow.com/a/16034262/3397630 Hope it was helpful ,thanks – Karthik Elumalai May 29 '17 at 16:26
  • I think `HttpVerbs` haven't been supported in .NET Core 1.1 yet so we have to use string array. The point is I can't make my method accept `GET`, so no `[AcceptVerbs("Get", "Post")]`. – MiP May 29 '17 at 17:00