Skip to main content
 首页 » 编程设计

c#之"System.Web.HttpException: Response is not available in this context"仅在 IIS 中

2024年01月24日29freeliver54

我在我的 global.asax.cs 应用程序启动中使用了 Http.Current.Response。当我在我的电脑上执行时,它工作正常,没有任何问题。然而,当我尝试将它放入带有 Windows Server 2008 R2 的 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 中执行。

谢谢

请您参考如下方法:

我不会在 Application_Start 中做面向请求/响应的事情。尝试在 BeginRequest 中执行此操作。