简体   繁体   English

如何为我的 asp.net MVC 站点的每个访问者添加 cookie?

[英]How do I add a cookie for every visitor to my asp.net MVC site?

I'm experimenting with an ASP.NET MVC 3 site, using razor as the view-engine.我正在尝试使用 ASP.NET MVC 3 站点,使用 razor 作为视图引擎。 I need to assign a cookie to every visitor of my site.我需要为我网站的每个访问者分配一个 cookie。 What would be the best place/way to do this?这样做的最佳地点/方式是什么? Please elaborate, because I'm very new at ASP.NET.请详细说明,因为我是 ASP.NET 的新手。

There are 3 ways to implement it without breaking mvc pattern:有 3 种方法可以在不破坏 mvc 模式的情况下实现它:

1 - Base controller class with specified behaviour at OnActionExecuting / OnActionExecuted / OnResultExecuting method (if this behavior is necessary across the entire web site) 1 - 基础 controller class 在OnActionExecuting / OnActionExecuted / OnResultExecuting方法中具有指定行为(如果此行为在整个 web 站点中是必需的)

2 - Create action filter with specified behaviour at OnActionExecuting / OnActionExecuted / OnResultExecuting methods: 2 - 在OnActionExecuting / OnActionExecuted / OnResultExecuting方法中创建具有指定行为的操作过滤器:

public class MyCookieSettingFilterAttribute : ActionFilterAttribute 
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        filterContext.HttpContext.Response.Cookies.Add(new HttpCookie(name, value));
    }
}

and

assign filter attribute to some controllers/actions (if this behavior is not necessary for all web site), for example为某些控制器/操作分配过滤器属性(如果所有 web 站点都不需要此行为),例如

[MyCookieSettingFilter]
public class MyHomeController : Controller
{
}

or或者

public class MyAccountController : Controller
{
    [MyCookieSettingFilter]
    public ActionResult Login()
    {
    }
}

3 - Create action filter with specified behaviour at OnActionExecuting / OnActionExecuted / OnResultExecuting methods and register it at global.asax - it will work for all actions of all controllers (if this behavior is necessary for all web site) 3 - 在OnActionExecuting / OnActionExecuted / OnResultExecuting方法中创建具有指定行为的操作过滤器,并将其注册到global.asax - 它适用于所有控制器的所有操作(如果所有 web 站点都需要此行为)

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
   filters.Add(new MyCookieSettingFilterAttribute());
}

I don't recommend to use Base Controller way, because it less extensible than Global Filter way.我不建议使用 Base Controller 方式,因为它的可扩展性不如全局过滤器方式。 Use different global filters for providing different independent global behaviors.使用不同的全局过滤器来提供不同的独立全局行为。

This would work regardless of what page the user first comes in at.无论用户首先进入哪个页面,这都会起作用。 You could inherit your controllers with a base controller and then add some information to the OnActionExecuting method您可以使用基本 controller 继承您的控制器,然后将一些信息添加到 OnActionExecuting 方法

public class BaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext context)
    {
        HttpCookie myCookie = Request.Cookies[keyOfSomeKind];

        if (myCookie == null)
        {
            HttpCookie newCookie
                = new HttpCookie(keyOfSomeKindy, "Some message");
            newCookie.Expires = DateTime.Now.AddMinutes(3);
            current.Response.Cookies.Add(newCookie);
        }

        base.OnActionExecuting(context);
    }
}

You already have session and the session cookie.您已经拥有 session 和 session cookie。

But if you need to write a specific value to a cookie you have access to the response stream from the controller但是,如果您需要将特定值写入 cookie,您可以访问来自 controller 的响应 stream

this.Response.Cookies.Add(); inside the controller (this isn't required) controller 内部(这不是必需的)

Setting a cookie should be done in your controller.应该在 controller 中设置 cookie。 You can set a cookie like so:你可以像这样设置一个cookie:

Response.Cookies.Add(new HttpCookie(cookieName, cookieValue));

If you need to get the value in your view, the best way would be to fetch it in your controller and stick it in a view model or viewstate:如果您需要在视图中获取值,最好的方法是在 controller 中获取它并将其粘贴在视图 model 或视图状态中:

var cookie = Response.Cookies[cookieName];
ViewData["CookieInfo"] = cookie.Value;

And in your view:在您看来:

@ViewData["CookieInfo"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM