MVC SessionStateAttribute不工作作为全局属性 [英] MVC SessionStateAttribute not working as Global Attribute

查看:459
本文介绍了MVC SessionStateAttribute不工作作为全局属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么设置SessionStateAttribute在MVC3一个全球性的过滤器?
在我的Global.asax我有这样的RegisterGlobalFilters方法。

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();
    }
}

但由于某些原因,它仍然让我使用的会话。但是如果我装点HomeController类本身带有的属性,我得到利用有关对象引用而空,我猜如果从未创建会话的目的是会话行的错误?

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?

推荐答案

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.sessionstateattribute.aspx\"><$c$c>SessionStateAttribute是不是动作过滤器的,所以你不能将其添加为一个全球性的行为过滤器。这是一个特殊的属性,它允许您与装饰你的控制器和有超过每个控制器的会话模式更细粒度的控制。

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.

要关闭全局的会议上为整个应用程序放在你的web.config以下内容:

To disable the session globally for the entire application put the following in your web.config:

<sessionState mode="Off" />

这篇关于MVC SessionStateAttribute不工作作为全局属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆