Cookies 如何从WebMethod更改cookie值?

Cookies 如何从WebMethod更改cookie值?,cookies,asp.net,Cookies,Asp.net,我想更改这个WebMethod末尾的cookie,它也被命名为“主题”。我怎样才能做到这一点?cookie必须在这里设置,而不是通过JavaScript。这是一项要求。谢谢您可以在webMethod中访问HttpContext,然后从那里访问响应对象 [WebMethod] public static void SetTheme(string theme) { Guid studentIdentifier = SessionData.LoggedInUser.Identifie

我想更改这个WebMethod末尾的cookie,它也被命名为“主题”。我怎样才能做到这一点?cookie必须在这里设置,而不是通过JavaScript。这是一项要求。谢谢

您可以在webMethod中访问HttpContext,然后从那里访问响应对象

  [WebMethod]
  public static void SetTheme(string theme)
  {
   Guid studentIdentifier = SessionData.LoggedInUser.Identifier;
   Student student = (Student)ItemFactory.GetItem(studentIdentifier);
   student.Theme = theme;
  }
HttpResponse对象允许您访问发送到浏览器的Cookie,其中包含以下响应:

var response = HttpContext.Current.Response;

这本书很好地解释了这一点。您也可以使用HttpContext.Current.request访问请求cookie。您可以在webMethod中访问HttpContext,并从中访问响应对象

  [WebMethod]
  public static void SetTheme(string theme)
  {
   Guid studentIdentifier = SessionData.LoggedInUser.Identifier;
   Student student = (Student)ItemFactory.GetItem(studentIdentifier);
   student.Theme = theme;
  }
HttpResponse对象允许您访问发送到浏览器的Cookie,其中包含以下响应:

var response = HttpContext.Current.Response;

这本书很好地解释了这一点。您也可以使用HttpContext.Current.request访问请求cookies,您是对的,我必须使用:HttpContext.Current.Response.cookies[“theme”]。Value=theme;谢谢。你说得对,我必须使用:HttpContext.Current.Response.Cookies[“theme”]。Value=theme;非常感谢。