C# 在对用户进行身份验证后,如何将变量传递给每个控制器

C# 在对用户进行身份验证后,如何将变量传递给每个控制器,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我正在研究一个分为年度会议的系统。用户可以转到并更改会话以查看过去的会话 如何将用户当前的yearId传递给每个控制器 我在想,我可以在身份验证上设置一个用户cookie,或者当用户手动更改会话时,使用这样的全局过滤器检查cookie public class MyTestAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterConte

我正在研究一个分为年度会议的系统。用户可以转到并更改会话以查看过去的会话

如何将用户当前的
yearId
传递给每个控制器

我在想,我可以在身份验证上设置一个用户cookie,或者当用户手动更改会话时,使用这样的全局过滤器检查cookie

public class MyTestAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpCookie cookie = filterContext.HttpContext.Request.Cookies["myCookie"];

        //do something with cookie.Value
        if (cookie!=null) 
        {
           filterContext.ActionParameters["YearId"] = cookie.Value;
        }
        else
        {
           // do something here
        }
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);
    }
}
下面是我如何使用上述过滤器:(或将其添加为全局过滤器)


使用这种方法,我必须向每个控制器添加yearId。非常感谢您的反馈。

这是太多的开销了


为什么不在会话中输入值?如果他们更改“会话”以查看过去的会话,只需修改会话中的变量。

您还可以为需要参数而不是筛选器的控制器创建基类:

public class MyBaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpCookie cookie = filterContext.HttpContext.Request.Cookies["myCookie"];

        //do something with cookie.Value
        if (cookie!=null) 
        {
           filterContext.ActionParameters["YearId"] = cookie.Value;
        }
        else
        {
           // do something here
        }
    }
}
或者您甚至可以创建一个强类型属性并将其设置为惰性,这样您就不必将其作为参数包含到每个操作方法中,也不必执行计算,除非您访问该属性:

public class MyBaseController : Controller
{
    private int? _yearId;

    protected int YearId
    {
        get
        {
             // Only evaluate the first time the property is called
             if (!_yearId.HasValue)
             {
                 // HttpContext is accessible directly off of Controller
                 HttpCookie cookie = HttpContext.Request.Cookies["myCookie"];

                 //do something with cookie.Value
                 if (cookie!=null) 
                 {
                      _yearId = int.Parse(cookie.Value);
                 }
                 else
                 {
                      // do something here
                 }
             }

             return _yearId.Value;
        }
    }
}

将变量设置为session我认为他不能(或者我不理解第一行)我更愿意避免使用session state,问题是什么?看来你的方法还可以。您可以编写一些基类,而不是标记所有控制器,但要键入的字符数相似。我更希望避免使用会话状态。您可以将当前年份存储在数据库中,然后进行查找以查看当前年份的设置。在这种情况下,您的方法很好。使用session可以避免每次请求都必须检索yearId。如果你不想这样做,你就没有其他选择了(除了按照上面的建议从数据库中获取数据)。@Rick B如何在会话中存储数据?@Bhushan根据应用程序的不同,有很多方法可以实现。最简单的是直接登录时或当yearId更改时-
Session[“yearId”]=value
我必须尝试一下这一点,听起来它可能适合我的情况
public class MyBaseController : Controller
{
    private int? _yearId;

    protected int YearId
    {
        get
        {
             // Only evaluate the first time the property is called
             if (!_yearId.HasValue)
             {
                 // HttpContext is accessible directly off of Controller
                 HttpCookie cookie = HttpContext.Request.Cookies["myCookie"];

                 //do something with cookie.Value
                 if (cookie!=null) 
                 {
                      _yearId = int.Parse(cookie.Value);
                 }
                 else
                 {
                      // do something here
                 }
             }

             return _yearId.Value;
        }
    }
}