tags:

views:

77

answers:

1

I recently started to look into asp.net mvc. Here is my issue.

Say every page on an application needs a variable set by the user, e.g. a date. If the user starts from url I provide, it is all good as I ask for that date and save it for the session. How can I redirect the user to the first page if they save the some other url (to a different controller and action).

In other words, I guess I am looking for something like [Authorize] attribute but on an application level.

Thanks for any help.

+3  A: 

I would probably create a base controller that all of my controllers derive from. In the base controller I'd override the OnActionExecuting method to check the session for the required variable. If the variable isn't present, I would set the ActionExecutingContext Result property to a RedirectToRouteResult to the appropriate controller/action to set the variable.

Another alternative is to create a custom FilterAttribute that you decorate the appropriate controllers/actions with that does basically the same thing. I would only do this if the filter was to apply only to certain controllers or actions and not all as you describe in your question.

tvanfosson
Thanks for the quick response. Overriding OnActionExecuted worked perfectly.
coderguy123