RedirectToAction gets ignored

梦想与她 提交于 2019-12-10 19:09:32

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/34261205/redirecttoaction-gets-ignored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!