作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
[WebMethod]
public static void SetTheme(string theme)
{
Guid studentIdentifier = SessionData.LoggedInUser.Identifier;
Student student = (Student)ItemFactory.GetItem(studentIdentifier);
student.Theme = theme;
}
我想更改此 WebMethod 末尾也名为“主题”的 cookie。我怎样才能做到这一点? cookie 必须在这里设置,而不是通过 JavaScript。这是一个要求。谢谢
最佳答案
您可以在 webMethod 中访问 HttpContext,并从那里访问响应对象。
var response = HttpContext.Current.Response;
HttpResponse 对象允许您访问随响应发送到浏览器的 cookie:
if(response.Cookies["theme"]!=null)
response.Cookies["theme"].Value = myValue;
MSDN documentation很好地解释了这一点。您也可以使用 HttpContext.Current.Request 访问请求 cookie
关于cookies - 如何从 WebMethod 更改 cookie 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4041031/
我是一名优秀的程序员,十分优秀!