gpt4 book ai didi

asp.net-mvc-3 - Mvc3 RouteConstraint - session 为空

转载 作者:行者123 更新时间:2023-12-01 01:30:48 24 4
gpt4 key购买 nike

在 Mvc3 中,我在实现 IRouteConstraint 接口(interface)时创建了自己的 RouteConstraint 类,这意味着我实现了 Match 函数。我遇到的唯一但严重的问题是,每当调用 Match 函数时, session 对象始终为 null。

我的简单代码如下:

public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (routeDirection != RouteDirection.IncomingRequest)
{
return true;
}

HttpSessionStateBase sessionBase = httpContext.Session; // will be null
HttpSessionState session = HttpContext.Current.Session; // this will be null either

return true;
}

我无法避免使用 session 对象,因为我需要登录管理员的“级别”/“类型”。我也不想在 cotroller 类中做我的事情,因为按时间维护会很麻烦。

谢谢,嘉宝

最佳答案

我在这里发布了一个关于 session 状态的类似问题的答案,如果其他人登陆这篇文章的话,我也会与之抗争。 BaseController 类也可能是您的答案,因为维护将仅在 1 个 Controller 中进行,其余的将继承:

session becoming null in MVC AuthorizeAttribute

希望它对以后的人有所帮助!

关于asp.net-mvc-3 - Mvc3 RouteConstraint - session 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7147997/

24 4 0