HttpContext.Current.Response一个静态方法中 [英] HttpContext.Current.Response inside a static method
本文介绍了HttpContext.Current.Response一个静态方法中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个静态内部类下面的静态方法。我的问题是可以安全使用HttpContext.Current.Response静态方法里面呢?我想是100%肯定它是线程安全的,并且只与调用线程相关..是否有人知道答案?
公共静态无效的setcookie(串cookieName,串cookieVal,System.TimeSpan TS)
{
试
{
的HttpCookie饼干=
新的HttpCookie(CookiePrefix + cookieName )
{值= cookieVal,过期= DateTime.Now.Add(TS)};
HttpContext.Current.Response.Cookies.Add(饼干);
}
赶上(例外)
{
的回报;
}
}
解决方案
是的其相当安全的。 HttContext.Current将收购正在执行的线程当前HttpContext。
它的一个共同的技术,不必通过上下文对象周围像流浪汉数据为您节省
I have the following static method inside a static class. My question is it safe to use HttpContext.Current.Response inside a static method? I want to be 100% sure that it is thread safe and is only associated with the calling thread.. Does anybody know the answer?
public static void SetCookie(string cookieName, string cookieVal, System.TimeSpan ts)
{
try
{
HttpCookie cookie =
new HttpCookie(CookiePrefix + cookieName)
{Value = cookieVal, Expires = DateTime.Now.Add(ts)};
HttpContext.Current.Response.Cookies.Add(cookie);
}
catch (Exception)
{
return;
}
}
解决方案
Yes its quite safe. HttContext.Current will acquire the current HttpContext from the thread that is executing.
Its a common technique and saves you from having to pass the context object around like "Tramp data".
这篇关于HttpContext.Current.Response一个静态方法中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文