gpt4 book ai didi

cookies - 如何从 WebMethod 更改 cookie 值?

转载 作者:行者123 更新时间:2023-12-02 01:21:20 26 4
gpt4 key购买 nike

  [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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com