【问题标题】:"System.Web.HttpException: Response is not available in this context" only in IIS“System.Web.HttpException:响应在此上下文中不可用”仅在 IIS 中
【发布时间】:2013-12-29 17:14:42
【问题描述】:

我在 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()中调用

标签: c# iis response global-asax httpcontext


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    有关详细信息,请在此处查看我的问题和接受的答案:

    Global ASAX - get the server name


    还要注意,您的代码中似乎存在一个逻辑错误 - 这将仅为应用程序启动时访问该站点的人设置 cookie - 这不会针对每个请求或每个会话运行。

    【讨论】:

      猜你喜欢
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      • 2013-01-09
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多