Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting TempData within a ActionFilterAttribute

I have a custom action filter, that inside the OnActionExecuting, depending on certain criteria, logs out a user and redirects them to the home page of the site. The (stripped back) code for the redirect part is below

  filterContext.Controller.TempData.Add("key", "Message");

  filterContext.Result = new RedirectResult("/");

As above, i am also setting a tempData message. Because the user has been logged out, when they hit the home page, the [Authorize] attribute will redirect them to the login GET page. On the login view, i am displaying any messages from within tempData. However in this situation the tempData is empty.

This is very similar behavior to how my login POST works (if invalid, it redirects to home, which redirects to login and displays the tempData message that was set in the Login post). This code can be seen below

   TempData.Add("key", errorMessage);

   return Redirect("/"));

The reason i am doing it this way, rather than redirecting specifically to the login page is because this code is distributed across many sites, so we dont know what the login GET url is.

Does anyone have any information as why this is working for the Login POST but not for the ActionFilter Redirect?

Edit:

If i remove the logout call within the custom action filter, the tempData is still set within the Home action - however this doesnt explain why it works for the Login POST but not the action filter?

like image 593
Rob Avatar asked Apr 22 '13 11:04

Rob


People also ask

How do you use TempData in razor view?

Passing the data from Controller to View using TempDataGo to File then New and select “Project” option. Then create the ASP.NET web application project as depicted below. Then select “Empty” and tick “MVC” then click OK. The project is created successfully.

How to use action filter in c#?

You can use an action filter, for instance, to modify the view data that a controller action returns. Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.

What is the use of actionfilters in mvc?

ASP.NET MVC provides Action Filters for executing filtering logic either before or after an action method is called. Action Filters are custom attributes that provide declarative means to add pre-action and post-action behavior to the controller's action methods.

Which filter execute first in asp net mvc?

as you can see from the below diagram, as soon as the controller starts execution through Action Invoker, Authentication and authorization filters are the very first filters to be triggered, followed by model binding which maps request and route data to action parameters.


1 Answers

So it turns out that when i was logging out the user from the system, i was also abandoning the session (calling HttpContextBase.Session.Abandon()) and also resetting the cookie session id. These were affecting the TempData behavior. By removing these calls, the tempData is now correctly set and displayed.

like image 77
Rob Avatar answered Nov 03 '22 21:11

Rob