“System.Web.HttpException: Response is not available in this context” only in IIS

倖福魔咒の 提交于 2019-12-08 03:56:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!