Use a IRouteConstraint or ActionFilterAttribute when validating session data against route data
I've been implementing a RouteConstraint recently whos responsibility is to Match a custom route value against some model data that is in the session.
What I've found开发者_StackOverflow中文版 is that the session is null for the routeDirection of IncomingRequest.
- Is the session not configured until the controller is created/initialized?
- If this is the case, is this type of function better suited to a filter applied at the controller level?
- Are there any other viable options?
The shame of it is that this validation does need to be applied on every route except for when the session is configured on first entry, so I thought a route constraint would be the best solution.
Thanks.
Since it has to happen on pretty much each call I would probably handle the controller's (or base controller's) OnActionExecuting and then set the ActionExecutingContext.Result to return a new RedirectToRouteResult with a RouteValueDictionary that contains the values you need. This could be encapsulated in an ActionFilter as well (controller implements IActionFilter).
It is not clear to me what exactly you are trying to implement (redirection to login page or rendering some message) so I can't give any good alternative solution but I can tell you that session is not accessible when IRouteConstraint is being evaluated because routing pipeline happens before session is loaded. E.g. routing might render page that doesn't require session at all.
精彩评论