我遇到了奇怪的情况。只有当字符串是硬编码时,设置的cookie才会持续存在。在asp.net mvc Cookie中未设置
public void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.HttpContext.Items["Theam"] != null)
{
var sTheam = filterContext.HttpContext.Items["Theam"].ToString();
HttpCookie theamCookie = new HttpCookie("TheamCookie");
theamCookie.Values.Add("TheamVal", sTheam);
theamCookie.Expires = DateTime.UtcNow.AddDays(5);
HttpContext.Current.Response.Cookies.Add(theamCookie);
}
}
不管我做什么cookie不会持久。只有在将sTheam替换为“丘比特”之类的值时,该值才会持续。那是
theamCookie.Values.Add("TheamVal", "cupid");
工作,没有别的。
任何人都可以对发生的事情有所了解吗?我筋疲力尽,完全没有选择。经过8个多小时的调试,我意识到这一点。但不知道为什么会发生这种情况。请帮忙。
更新:以下是CookieFilter。这是一个ASP.NET MVC应用程序。
public class CookieFilter : IActionFilter
{
//private const string sTheamCookieName = "TheamCookie";
public void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.HttpContext.Items["TheamToBrowser"] != null)
{
var sTheam = ((string)(filterContext.HttpContext.Items["TheamToBrowser"])).ToString(CultureInfo.InvariantCulture);
HttpCookie theamCookie = new HttpCookie("TheamCookie");
theamCookie.Values["TheamVal"] = "shamrock";
theamCookie.Expires = DateTime.UtcNow.AddDays(5);
filterContext.HttpContext.Response.Cookies.Add(theamCookie);
}
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContextBase context = filterContext.HttpContext;
HttpCookie theamCookie = context.Request.Cookies["TheamCookie"];
if (theamCookie == null)
context.Items["TheamFromBrowser"] = "default";
else
{
if (string.IsNullOrEmpty(theamCookie.Value))
{
context.Items["TheamFromBrowser"] = "default";
}
else
context.Items["TheamFromBrowser"] = theamCookie.Values["TheamVal"].ToString();
}
}
}
如果以这种方式添加cookie,会发生什么情况'Response.Cookies.Add(theamCookie);'这个值是如何声明的? 'TheamCookie'sis是一个常量字符串..也许这是设置为string.Empty某处.. – MethodMan 2014-11-20 17:28:32
filterContext.HttpContext.Current.Response.Cookies.Add(theamCookie) – 2014-11-20 18:26:51
http://stackoverflow.com/questions/6227222/我怎么做我添加一个cookie为每个访客到我的ASP网络mvc网站 – MethodMan 2014-11-20 19:51:47