[英]How do I redirect from one Async Action to Another (MVC5)
I have an Admin section to my site, where I want admin people to be able to add new users. 我的网站上有一个“管理员”部分,我希望管理员可以在其中添加新用户。 I also have a separate Register Link, which works.
我也有一个单独的Register Link,它可以工作。
I want to use ajax
to update my admin section. 我想使用
ajax
更新我的管理部分。 So the plan is: 因此,计划是:
AdminController
checks if it is valid and then redirects to the AuthenticationController
method to create the user AdminController
检查它是否有效,然后重定向到AuthenticationController
方法以创建用户 AuthController
returns success, then reload the list of all users AuthController
返回成功,则重新加载所有用户的列表 When I debug the code, I hit the Admin controller, but I cannot step into the AuthController
to see why it is failing. 当我调试代码时,我点击了Admin控制器,但是我无法进入
AuthController
来查看为什么失败。 (It all compiles OK) (都可以编译)
I assume it is something to do with the fact that my two methods are both async, but I cannot await the redirect from one to the other. 我认为这与我的两个方法都异步的事实有关,但是我无法等待从一个方法重定向到另一个方法。
Briefly, the code is; 简单来说,代码是:
[Authorize(Roles = "AccessAllAreas")]
[HttpPost]
[ValidateAntiForgeryToken()]
public async Task<PartialViewResult> CreateNewUser(ApplicationUserViewModel avm)
{
if (ModelState.IsValid)
{
try
{
var foo = RedirectToAction("RegisterAsyncFromOtherController", "Account", avm);
....
}
[Authorize(Roles = "AccessAllAreas")]
[ValidateAntiForgeryToken()]
public async Task<ActionResult> RegisterAsyncFromOtherController(ApplicationUserViewModel model)
{
if (ModelState.IsValid)
{
....
}
}
A break point on the above if (Modelstate.IsValid)
is never hit. if (Modelstate.IsValid)
从未被击中,则上面的if (Modelstate.IsValid)
。 However a breakpoint and then step into on the call to RegisterAsyncFromOtherController()
accesses one of the properties on the view model, then it returns to CreateNewUser
. 但是,断点然后进入对
RegisterAsyncFromOtherController()
的调用将访问视图模型上的属性之一,然后返回到CreateNewUser
。 (Which is what makes me suspect that the async is not working correctly). (这就是让我怀疑异步无法正常工作的原因)。
Is there anything wrong with the concept I have here, and what is causing the debug behaviour I am seeing? 我这里的概念有什么问题吗,是什么导致我看到的调试行为?
a RedirectToAction
should be returned to the response so the browser of the visitor can redirect to the given url. RedirectToAction
应该返回到响应,以便访问者的浏览器可以重定向到给定的URL。 Only after that has happened will you hit the RegisterAsyncFromOtherController
action. 只有在这种情况发生后,您才能执行
RegisterAsyncFromOtherController
操作。
It seems that you're confusing RedirectToAction()
with Response.Redirect()
. 似乎您将
RedirectToAction()
与Response.Redirect()
混淆了。
RedirectToAction()
returns a RedirectToRouteResult
(which inherits from ActionResult
), and you have to return it to the caller in order for it to have any effect. RedirectToAction()
返回一个RedirectToRouteResult
(继承自ActionResult
),并且您必须将其返回给调用方,以使其生效。 If you don't return it, RedirectToAction()
won't do anything? 如果不返回,
RedirectToAction()
不会执行任何操作?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.