MVC SessionStateAttribute 不能用作全局属性
如何在 MVC3 中将 SessionStateAttribute 设置为全局过滤器? 在我的 Global.asax 中,我在 RegisterGlobalFilters 方法中有这个。
filters.Add(new SessionStateAttribute(SessionStateBehavior.Disabled));
在我的家庭控制器中我有这个。
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
Session["Blend"] = "Will it blend?";
return View();
}
public ActionResult About()
{
return View();
}
}
但由于某种原因它仍然让我使用会话。但是,如果我用该属性装饰 HomeController 类本身,我会在使用 Session 的行上收到一个关于对象引用为空的错误,我猜测如果从未创建 Session,这是有意的?
我开始怀疑我的项目是否有问题。我遇到过像这样的小问题,其标准行为应该可以正常工作。
还有其他人遇到过这样的问题吗?
How do you setup SessionStateAttribute as a global filter in MVC3?
In my Global.asax I have this in the RegisterGlobalFilters method.
filters.Add(new SessionStateAttribute(SessionStateBehavior.Disabled));
And in my home controller I have this.
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
Session["Blend"] = "Will it blend?";
return View();
}
public ActionResult About()
{
return View();
}
}
But for some reason it still lets me use the Session. However if I decorate the HomeController class itself with the attribute, I get an error on the line utilizing the Session about a Object reference being null, which I'm guessing is intended if the Session is never created?
I am starting to wonder if there is something wrong with my project. I've been getting little problems like this one with standard behavior that are supposed to just work.
Anyone else had problems with things like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SessionStateAttribute
是不是 操作过滤器,因此您无法将其添加为全局动作过滤器。它是一个特殊的属性,允许您装饰控制器并对每个控制器的会话模式进行更细粒度的控制。要为整个应用程序全局禁用会话,请将以下内容放入您的 web.config 中:
SessionStateAttribute
is not an action filter, so you cannot add it as a global action filter. It's a special attribute which allows you to decorate your controllers with and have a more fine grained control over the session mode per controller.To disable the session globally for the entire application put the following in your web.config: