C# HttpContextBase.Session和HttpContext.Current.Session在IRouteConstraint MVC4上都为空
我无法访问自定义RootConstraint类上的会话,它被设置为空。我已经搜索过了,但找不到解决方案 编辑 以下是httpContext参数在即时窗口上的外观。梅提出了一个想法C# HttpContextBase.Session和HttpContext.Current.Session在IRouteConstraint MVC4上都为空,c#,asp.net-mvc,session,asp.net-mvc-4,C#,Asp.net Mvc,Session,Asp.net Mvc 4,我无法访问自定义RootConstraint类上的会话,它被设置为空。我已经搜索过了,但找不到解决方案 编辑 以下是httpContext参数在即时窗口上的外观。梅提出了一个想法 httpContext {System.Web.HttpContextWrapper} [System.Web.HttpContextWrapper]: {System.Web.HttpContextWrapper} AllErrors: null AllowAsyncDuringSyncSta
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
方法在跟踪时执行两次。在第一次执行中,routeddirection
参数设置为System.Web.Routing.routeddirection.UrlGeneration
,此时会话不为空。但是当第二次执行时,routeddirection
参数被设置为System.Web.Routing.routeddirection.IncomingRequest
,会话为空。为什么?通常您应该可以访问会话。但是在ASP.NET MVC应用程序中,请避免使用HttpContext.Current
静态属性。使用提供给您的HttpContextBase
抽象:
public class AdminRootConstraint : IRouteConstraint
{
public bool Match
(
HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection
)
{
if ((string) values["controller"] == "Login")
{
return true;
}
object isAuthorized = httpContext.Session["IsAuthorized"];
return (isAuthorized != null && (bool)isAuthorized);
}
}
在本例中,
httpContext.Session
不会为空。可以为空的是httpContext.Session[“IsAuthorized”]
,如果您从未在会话中设置此键的值,这将是正常的。第一次调用匹配是因为MVC正在构建它将导航用户浏览器的URL(通过发送HTTP 302)。这一次,你有会话只是偶然的,因为它存在于那个时刻
第二次调用Match是在请求到达浏览器时。此时间会话不存在,因为路由逻辑是在加载会话之前执行的。例如,路由可能呈现根本不需要会话的页面
总之,会话在IRouteConstraint中不可访问,不应使用。它是
httpContext.session
null还是httpContext.session[“IsAuthorized”]
?如果httpContext.Session
为null,我会非常惊讶。httpContext.Session绝对为null。我也不明白。我不知道我是否错过了一个配置或什么。我怀疑你错过了一个配置。ASP.NET会话应始终可用。至少在HttpContext中它不会为null。尝试从头开始创建一个新的ASP.NET MVC项目,看看是否可以重现这个问题。然后发布你的精确步骤,让我们重现错误。我从来没有遇到过httpContext.Session
将为空的情况。我刚刚尝试了一个新的mvc4项目,但没有任何变化。我对我的问题做了一些修改。希望它能给你一个想法。
public class AdminRootConstraint : IRouteConstraint
{
public bool Match
(
HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection
)
{
if ((string) values["controller"] == "Login")
{
return true;
}
object isAuthorized = httpContext.Session["IsAuthorized"];
return (isAuthorized != null && (bool)isAuthorized);
}
}