我无法在我的自定义 RootConstraint 类上访问 Session,它设置为 null。我已经搜索但找不到解决方案。
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"]; } }
编辑
这是 httpContext 参数在即时窗口上的样子。梅给出了一个主意。
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
编辑 2
我正在使用RedirectToAction
同一区域中的操作中的方法,并且该Match
方法在我跟踪时执行了两次。第一次执行时,routeDirection
参数设置为System.Web.Routing.RouteDirection.UrlGeneration
,此时 Session 不为空。但是当第二次执行时,routeDirection
参数设置为System.Web.Routing.RouteDirection.IncomingRequest
并且会话为空。为什么?