Setting TempData to ActionFilterAttribute

I have a custom action filter that inside OnActionExecuting , depending on certain criteria, displays the user and redirects him to the websiteโ€™s home page. Code (taken back) for the redirect part below

  filterContext.Controller.TempData.Add("key", "Message"); filterContext.Result = new RedirectResult("/"); 

As above, I also set the tempData message. Since the user is logged out, when they get to the main page, the [Authorize] attribute will redirect them to the GET login page. In the login view, I show any messages from tempData. However, in this situation tempData is empty.

This is very similar to how my POST login works (if it is not valid, it is redirected to the house, which redirects the login and displays the tempData message that was set in the "Login" message). This code can be seen below.

  TempData.Add("key", errorMessage); return Redirect("/")); 

The reason I do it this way, and not specifically redirect to the login page, is because this code is distributed to many sites, so we donโ€™t know what the login URL is.

Does anyone have any info on why this works for POST login but not for ActionFilter Redirection?

Edit:

If I remove the exit call from the custom action filter, tempData is still set in the Home action, but that doesn't explain why it works for POST login, but not for the action filter?

+7
source share
2 answers

So, it turns out that when I HttpContextBase.Session.Abandon() out, I also refused the session (called HttpContextBase.Session.Abandon() ) and also reset the cookie session ID. This affected the behavior of TempData. By removing these calls, tempData is now properly configured and displayed.

+4
source

setting the result to new RedirectResult("/") will stop the current server processing and send a response to the client that tells the client to request a new URL - the one you said in RedirectResult. The second query is then different and does not contain the values โ€‹โ€‹from the previous processing. Try using Redirect("/"); or Server.Transfer("/"); to serve a new route in the same client request.

-one
source

All Articles