views:

6

answers:

1

Hi, I have this:

  [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 requirement. Thank you

A: 

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

Maupertuis
You are right, I had to use:HttpContext.Current.Response.Cookies["theme"].Value = theme;Thank you.
Bogdan Andrei Craciun