开发者

How can I change a cookie value from a WebMethod?

  [WebMethod]
  public static void SetTheme(string theme)
  {
   Guid studentIdentifier = SessionData.LoggedInUser.Identifier;
   Student student = (Student)ItemFactory.GetItem(studentIdentifier);
   student.Theme = theme;
  }

And I want to change the cookie that is also named "theme", at the end of this WebMethod. How can I accomplish that? The cookie has to be set here, not through JavaScript. That is a requi开发者_如何学Crement. Thank you


You can access the HttpContext in your webMethod, and from there, acess the response object.

var response = HttpContext.Current.Response;

The HttpResponse object allows you to access the cookies sent to the browser with the response:

if(response.Cookies["theme"]!=null)
  response.Cookies["theme"].Value = myValue;

the MSDN documentation makes a good job explaining it. You can access the request cookies too, using HttpContext.Current.Request

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜