0

UPDATE: I remove all of the async calls and made it completely synchronous and I am still experiencing this issue so it is not an issue with async.

So I have a LoginLayout in my layouts. Its not the main layout file. When a user logs in we determine some conditional values in the datababase and then redirect them accordingly to an intermediary page or directly to our dashboard.

If our user gets directed to the intermediary page first, and in this instance they click a submit button that posts to an action, then everything works as expected. But if we dont load the intermediary page and instead redirect to a different action which calls the same Logon function. They should go straight to the dashboard aka do not return a view instead return a RedirectToAction, the same function breaks the layout of the Dashboard.

I can actually see MVC is rendering the main _layout file INSIDE the LoginLayout files @RenderBody() method. and the URL does not change to /Dashboard which is also strange.

It is also worth noting that I have tried RedirectToAction, RedirectToRoute, Redirect all do the exact same thing. Also once the garbled page loads if I manually navigate to /Dashboard it renders correctly.

Here is the code

//In my Login controller PostLogin method
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogonPost(LoginModel model)
{
    //validate login and if all good then
    return RedirectToAction("Index", "Intermediary");
}


 //Intermediary Controller this one does not work if called page is loaded wierd as described
 public async Task<ActionResult> Index()
 {                
     await Task.Run(() =>
     {
         //grab database stuff in here set session vars etc

     });

     return await PostLogonConnecting();                    
  }

This one gets called as a post from the page when they click a button and works fine

public async Task<ActionResult> PostCompany(CompaniesListModel model)
{
     //do db stuff 
    return await PostLogonConnecting();    
}

This PostLogonConnecting is in my base class so it can be called from multiple controllers.

public async Task<RedirectToActionResult> PostLogonConnecting()
{                
    await Task.Run(() =>
    {
        //do db stuff in here
    });    

    return RedirectToAction("Index", "Dashboard"); 
}
Mike Brind
  • 28,238
  • 6
  • 56
  • 88
Joe
  • 234
  • 5
  • 18
  • Place breakpoints everywhere, follow the flow inside a debugger. Including your views. – mxmissile Feb 07 '20 at 22:11
  • So I do this but I dont see what the issue is, after redirecttoaction is called it redirects and hits my new view on dashboard which at the top of index.cshtml has @{ Layout = null; ViewBag.Title = "Welcome To Site"; Layout = (ViewBag.isPartialView) ? null : "~/Views/Shared/_Layout.cshtml"; } It is getting there as the breakpoint is hit. So I would expect this to override the previous _LoginLayout.cshtml layout.But even after this runs it renders inside the _loginLayout.cshtml page inside the RenderBody() of the Login layout.instead of just rendering the one layout. – Joe Feb 08 '20 at 06:23

1 Answers1

0

so I found out this is related to the AjaxHelper. (Not added by me) so I was not sure how that works. Some googling about that led me to this answer. That was a pain to figure out but at least it's resolved!

How to get an ASP.NET MVC Ajax response to redirect to new page instead of inserting view into UpdateTargetId?

Joe
  • 234
  • 5
  • 18