无论HttpContextBase.Session和HttpContext.Current.Session上IRouteConstraint MVC4空 [英] Both HttpContextBase.Session and HttpContext.Current.Session are null on IRouteConstraint MVC4
问题描述
我不能在我的自定义RootConstraint类到达会议,IT'IS设置为null。我已经搜查,但未能找到解决办法。
公共类AdminRootConstraint:IRouteConstraint
{
公共BOOL匹配
(
HttpContextBase HttpContext的,
路线路线,
字符串参数名称,
RouteValueDictionary值,
routeDirection routeDirection
)
{
如果((字符串)值[控制器] ==登录)
返回true; 返回HttpContext.Current.Session = NULL&放大器;!&安培; (布尔)HttpContext.Current.Session [IsAuthorized];
}
}
块引用>修改
下面是HttpContext的参数怎么看起来像在即时窗口。五月给出了个主意。
的HttpContext
{} System.Web.HttpContextWrapper
[System.Web.HttpContextWrapper]:{} System.Web.HttpContextWrapper
AllErrors:空
AllowAsyncDuringSyncStages:假的
应用:{} System.Web.HttpApplicationStateWrapper
ApplicationInstance:{} ASP.global_asax
异步preloadMode:无
缓存:{}的System.Web.Caching.Cache
CurrentHandler:空
CurrentNotification:ResolveRequestCache
错误:空
处理器:空
IsCustomErrorEnabled:假的
IsDebuggingEnabled:真
IsPostNotification:真
IsWebSocketRequest:假的
IsWebSocketRequestUpgrading:假的
项目:计数= 00000000
PageInstrumentation:{} System.Web.Instrumentation.PageInstrumentationService
previousHandler:空
简介:空
要求:{System.Web.Htt prequestWrapper}
回应:{System.Web.Htt presponseWrapper}
服务器:{} System.Web.HttpServerUtilityWrapper
会议:空
SkipAuthorization:假的
ThreadAbortOnTimeout:真
时间戳:{2013年9月14日16点52分53秒}
跟踪:{} System.Web.TraceContext
网友:{} System.Security.Principal.WindowsPrincipal
WebSocketNegotiatedProtocol:空
WebSocketRequestedProtocols:空编辑2
我用
RedirectToAction
方法从同一地区的行动,而匹配
方法执行两倍我追查。在firts执行过程中,routeDirection
参数设置为System.Web.Routing.RouteDirection.UrlGeneration
,到时候会话不为空。但是当第二次执行大作,routeDirection
参数设置为System.Web.Routing.RouteDirection.IncomingRequest
和会议为空。为什么呢?解决方案第一次比赛被称为是因为MVC正在建设的网址,并向它导航的用户的浏览器(通过发送HTTP 302)。这个时候你偶然有会议只是因为它在那一刻存在。
第二次比赛被称为是当请求到达浏览器。这次会议是不是present因为路由逻辑会被加载之前执行。例如。路由可能会使不需要届所有页面。
要结束,会话不在IRouteConstraint访问,不应使用。
I can't reach Session on my custom RootConstraint class, it'is set to null. I've searched but couldn't find a solution.
public class AdminRootConstraint : IRouteConstraint { public bool Match ( HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection ) { if ((string) values["controller"] == "Login") return true; return HttpContext.Current.Session != null && (bool)HttpContext.Current.Session["IsAuthorized"]; } }
Edit
Here is how the httpContext parameter looks like on immediate window. May gives an idea.
httpContext {System.Web.HttpContextWrapper} [System.Web.HttpContextWrapper]: {System.Web.HttpContextWrapper} AllErrors: null AllowAsyncDuringSyncStages: false Application: {System.Web.HttpApplicationStateWrapper} ApplicationInstance: {ASP.global_asax} AsyncPreloadMode: None Cache: {System.Web.Caching.Cache} CurrentHandler: null CurrentNotification: ResolveRequestCache Error: null Handler: null IsCustomErrorEnabled: false IsDebuggingEnabled: true IsPostNotification: true IsWebSocketRequest: false IsWebSocketRequestUpgrading: false Items: Count = 0x00000000 PageInstrumentation: {System.Web.Instrumentation.PageInstrumentationService} PreviousHandler: null Profile: null Request: {System.Web.HttpRequestWrapper} Response: {System.Web.HttpResponseWrapper} Server: {System.Web.HttpServerUtilityWrapper} Session: null SkipAuthorization: false ThreadAbortOnTimeout: true Timestamp: {14.09.2013 16:52:53} Trace: {System.Web.TraceContext} User: {System.Security.Principal.WindowsPrincipal} WebSocketNegotiatedProtocol: null WebSocketRequestedProtocols: null
Edit 2
I'm using
RedirectToAction
method from an action in the same area, and theMatch
method executes twice as I trace. In the firts execution, therouteDirection
parameter is set toSystem.Web.Routing.RouteDirection.UrlGeneration
, and that time the Session is not null. But when the second execution comes up, therouteDirection
parameter is set toSystem.Web.Routing.RouteDirection.IncomingRequest
and the session is null. Why?解决方案First time Match is called is because MVC is building URL to which it will navigate users's browser (by sending HTTP 302). This time you have session by accident simply because it exists in that moment.
Second time Match is called is when request comes to browser. This time session is not present because routing logic is executed before session is loaded. E.g. routing might render page that doesn't require session at all.
To conclude, session is not accessible in IRouteConstraint and should not be used.
这篇关于无论HttpContextBase.Session和HttpContext.Current.Session上IRouteConstraint MVC4空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!