gpt4 book ai didi

asp.net - 全局ASAX - 获取服务器名称

转载 作者:行者123 更新时间:2023-12-03 21:04:36 29 4
gpt4 key购买 nike

谁能告诉我是否有办法在 global.asax 的 Application_Start 事件中获取我网站的域名?

通常我只是从 Context.Request.ServerVariables["SERVER_NAME"] 获取它,但这不可用。理想情况下,我还希望从启动应用程序的请求中获取 URL。

嗯 - 从下面的答案来看,使用 IIS7 似乎在这里有所不同。这是新的,现在有设计指南来尝试阻止你这样做:

IIS Blog

最佳答案

您可以访问 Context透静HttpContext.Current成员。

HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
HttpContext.Current.Request.Url;

编辑,根据你的一些评论,我做了一些额外的研究

This error is due to a design change in the IIS7 Integrated pipeline that makes the request context unavailable in Application_Start event. When using the Classic mode (the only mode when running on previous versions of IIS), the request context used to be available, even though the Application_Start event has always been intended as a global and request-agnostic event in the application lifetime. Despite this, because ASP.NET applications were always started by the first request to the app, it used to be possible to get to the request context through the static HttpContext.Current field.



所以你有两个选择
  • 更改您的应用程序代码以不使用请求上下文(推荐)。
  • 将应用程序移至经典模式(不推荐)。

  • http://mvolo.com/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-applicationstart/

    关于asp.net - 全局ASAX - 获取服务器名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1790457/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com