我怎样才能改变从一个WebMethod cookie的值? [英] How can I change a cookie value from a WebMethod?

查看:210
本文介绍了我怎样才能改变从一个WebMethod cookie的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

和我想改变也命名为主旋律的饼干,在这个的WebMethod的结束。我怎样才能做到呢?该Cookie已在此处设置,而不是通过JavaScript。这是一个要求。谢谢

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

推荐答案

您可以访问的HttpContext在你的WebMethod,并从那里,接取响应对象。

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

var response = HttpContext.Current.Response;

该型Htt presponse对象允许您访问发送到具有响应浏览器的cookie:

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;

MSDN文档使一个很好的工作,解释它。您可以访问请求过于饼干,用HttpContext.Current.Request

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

这篇关于我怎样才能改变从一个WebMethod cookie的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆