简体   繁体   English

如何从一个异步操作重定向到另一个(MVC5)

[英]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: 因此,计划是:

  1. Admin fills in form of user info 管理员填写用户信息表格
  2. AdminController checks if it is valid and then redirects to the AuthenticationController method to create the user AdminController检查它是否有效,然后重定向到AuthenticationController方法以创建用户
  3. If the AuthController returns success, then reload the list of all users 如果AuthController返回成功,则重新加载所有用户的列表
  4. return this to the partial to update the screen. 将其返回到局部以更新屏幕。

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.

相关问题 如何在MVC5中将一个动作重定向到另一个动作? - How to redirect one action to another action in MVC5? 完成动作后,如何在ASP.NET MVC中将某人从一个动作重定向到另一个动作? - How do I redirect someone from one action to another in ASP.NET MVC when an action has completed? 如何将page.ASPX重定向到MVC5中的/ - How do I Redirect page.ASPX to / in MVC5 如何从一个 MVC5 应用程序获取访问令牌到另一个 MVC5 应用程序 - How to get access token from one MVC5 application to another MVC5 application 如何从一个动作重定向到另一个动作,但在MVC 3中提供其参数? - How to redirect from an action to another action but provide its parameters in MVC 3? 如何从MVC4中的异步动作重定向到另一个动作 - How can I redirect to another action from my Asynchronous Action in MVC4 MVC5-如何为动作参数类型定义反序列化行为? - MVC5 - How do I define deserialization behavior for action parameter type? 如何从ASP.NET MVC中的同一操作重定向到另一个视图或外部站点? - How can I redirect to either another view or an external site from the same action in ASP.NET MVC? 如何重定向到MVC网站我的父母行动? - How do I redirect to my parent action in MVC site? MVC5阻止除本地主机之外的所有主机的操作 - MVC5 block Action from all but localhost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM