开发者

Use a IRouteConstraint or ActionFilterAttribute when validating session data against route data

开发者 https://www.devze.com 2023-02-12 13:14 出处:网络
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.

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.

  1. Is the session not configured until the controller is created/initialized?
  2. If this is the case, is this type of function better suited to a filter applied at the controller level?
  3. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号