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
精彩评论