C# 动作过滤器中的会话变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14735185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:52:13  来源:igfitidea点击:

Session variables in action filter

c#asp.netasp.net-mvcasp.net-mvc-3asp.net-mvc-4

提问by Jeff

I have an action filter checks whether or not a session variable IDis set. For development purposes, I have manually set this variable prior to the check.

我有一个动作过滤器检查是否ID设置了会话变量。出于开发目的,我在检查之前手动设置了这个变量。

public class MyActionFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext context)
        {

            context.HttpContext.Session.Add("ID", 123123);

            int ID = (int)context.HttpContext.Session.Contents["ID"];
            var rd = context.HttpContext.Request.RequestContext.RouteData;

            TED _db = new TED();

            //if not in DB
            if (_db.Users.Find(ID) == null && rd.GetRequiredString("action") != "NoAccess")
            {
                RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
                redirectTargetDictionary.Add("action", "NoAccess");
                redirectTargetDictionary.Add("controller", "Home");
                redirectTargetDictionary.Add("area", "");

                context.Result = new RedirectToRouteResult(redirectTargetDictionary);
            }

            base.OnActionExecuted(context);
        }
    }

As far as I understand, this code is run prior to any page, this Session["ID"]is always set. The site works fine if I am consistently testing, but it seems to break if I leave it for a while then try to continue testing. Here is the error I get:

据我了解,此代码在任何页面之前运行,这Session["ID"]始终是设置的。如果我一直在测试,该站点就可以正常工作,但是如果我将其放置一段时间然后尝试继续测试,它似乎会中断。这是我得到的错误:

int UserID = (int)Session.Contents["ID"];
System.NullReferenceException: Object reference not set to an instance of an object.

Initially I thought the session may simply be expiring, but prior to any page loading, Session["ID"]should be getting set. What is the issue here?

最初我认为会话可能只是到期,但在任何页面加载之前,Session["ID"]应该设置。这里有什么问题?

采纳答案by Dave Alperovich

you're implementing your actionfilter's on OnActionExecutedmethod which executes AFTER your action method

您正在实现您的 actionfilter 的 onOnActionExecuted方法,该方法在您的操作方法之后执行

You should implement the OnActionExecutingmethod

你应该实现这个OnActionExecuting方法

public class MyActionFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {

            context.HttpContext.Session.Add("ID", 123123);

            int ID = (int)context.HttpContext.Session.Contents["ID"];
            var rd = context.HttpContext.Request.RequestContext.RouteData;

            TED _db = new TED();

            //if not in DB
            if (_db.Users.Find(ID) == null && rd.GetRequiredString("action") != "NoAccess")
            {
                RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
                redirectTargetDictionary.Add("action", "NoAccess");
                redirectTargetDictionary.Add("controller", "Home");
                redirectTargetDictionary.Add("area", "");

                context.Result = new RedirectToRouteResult(redirectTargetDictionary);
            }

            base.OnActionExecuting(context);
        }
    }