Access session data from custom RouteHandler?

…衆ロ難τιáo~ 提交于 2019-12-08 13:32:52

问题


I am building onto the example found here:

http://www.mikesdotnetting.com/Article/126/ASP.NET-MVC-Prevent-Image-Leeching-with-a-Custom-RouteHandler

What I would like to do now is have some logic that checks for certain data in session before allowing the valid image to show. Is it possible to access session data from within the custom RouteHandler or any other type of persisted data?


回答1:


You have access the session right from the requestContext:

private static void ProcessRequest(RequestContext requestContext)
{
   var session = requestContext.HttpContext.Session;



回答2:


I have published solution for this problem as answer to another question.

Look at the application life cycle overview (https://msdn.microsoft.com/en-us/library/bb470252(v=vs.140).aspx), particulary the row The request is processed by the HttpApplication pipeline. in the table Life Cycle Stages.

Whereas the function GetHttpHandler of your IRouteHandler object is invoked in the phase 10 (Raise the MapRequestHandler event.) of the pipeline, Session is restored in the phase 12 (Raise the AcquireRequestState event.). That is why you cannot access Session variables during the GetHttpHandler function and RequestContext.HttpContext.Session is always null.



来源:https://stackoverflow.com/questions/3122491/access-session-data-from-custom-routehandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!