Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RedirectToAction gets ignored

I am using ASP.NET 5. Using the browser Chrome.

My Controller has the following Action Method

public async Task<IActionResult> Index()
{
    return View();
}

[HttpPost]
public IActionResult DoSomething()
{
    //do something
    return RedirectToAction("Index");
}

When I run

http://localhost:59693/MyArea/MyController/DoSomething

via a POST in Index

and have a breakpoint over the line

 return RedirectToAction("Index");

it just ignores the line and goes to the next line without calling the Index action method.

and displays in the browser

http://localhost:59693/MyArea/MyController/DoSomething

with a blank screen.

Surely if you have a return statement then it immediately returns from that method and doesn't jump to the next line. Really odd.

I even tried to the full

 return RedirectToAction("Index","MyController,new {area="MyArea"});

When I put a breakpoint on my Index Action Method it never gets hit.

I even tried

 return Redirect("http://www.google.com");

It still displays

http://localhost:59693/MyArea/MyController/DoSomething

Some bug in ASP.NET 5?

How to I call an action method from an action method in the same controller if the above doesn't work?

like image 390
zoaz Avatar asked May 18 '26 20:05

zoaz


1 Answers

I changed my DoSomething action method to be asynchronous and added an await clause

[HttpPost]
public async Task<IActionResult> DoSomething()
{
    await _db.callsql();
    //do something start

    return RedirectToAction("Index");
}

The issue seems to be because the actionmethod Index was asynchronous but DoSomething not, combined with me stepping through the code.

like image 94
zoaz Avatar answered May 21 '26 09:05

zoaz