问题
i have used the Http.Current.Response
in my global.asax.cs Application Start. It is working fine without any issue when i execute in my PC. How ever when i try to put it in IIS with Windows Server 2008 R2, i find that it is giving the following error.

the code for it is
public static void SetCookie(string key, string value, int dayExpires)
{
HttpCookie encodedCookie = HttpSecureCookie.Encode(new HttpCookie(key, value));
encodedCookie.Expires = DateTime.Now.AddDays(dayExpires);
HttpContext.Current.Response.Cookies.Remove(key);
HttpContext.Current.Response.Cookies.Add(encodedCookie);
}
I wanted to trace out why it is getting executed in my system, but not in IIS.
Thanks
回答1:
I would not do request/response oriented things in Application_Start. Try doing it in BeginRequest
.
回答2:
The request context is not available in application start when running in integrated mode in IIS7.
Please see my question and the accepted answer here for details:
Global ASAX - get the server name
Would also note that there seems to be a logical bug in your code - this will set the cookie only for the person that hits the site when the application starts - this will not run for every request, or every session.
来源:https://stackoverflow.com/questions/20524093/system-web-httpexception-response-is-not-available-in-this-context-only-in-ii