“System.Web.HttpException:响应在此上下文中不可用”仅在IIS中

问题描述:

我在我的global.asax.cs Application Start中使用了Http.Current.Response。当我在我的电脑中执行时,它工作正常,没有任何问题。如何,当我尝试把它与Windows Server 2008 R2的IIS,我发现它给出了以下错误。 enter image description here“System.Web.HttpException:响应在此上下文中不可用”仅在IIS中

它的代码是

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); 
    } 

我想描绘出它为什么在我的系统得到执行,而不是在IIS。

感谢

+0

这个叫什么时候? –

+0

它被称为Application_Start() –

我不会做的请求/响应导向的东西的Application_Start。试试BeginRequest

+0

谢谢,我会试试 –

当在IIS7中以集成模式运行时,请求上下文在应用程序启动时不可用。

请参阅我的问题和接受的答案查看详情:

Global ASAX - get the server name


还要注意,似乎是在你的代码的逻辑错误 - 这将设置cookie只为在应用程序启动时点击该网站的人 - 这不会针对每个请求或每个会话运行。

+0

谢谢,我会试试看 –