如何在不使用 HttpContext.Current.Request 的情况下获取 ASP .NET 中的主机域名?

发布于 2024-08-19 22:09:20 字数 471 浏览 2 评论 0原文

我有一个在 IIS7 上运行的 ASP .Net 应用程序。我正在使用网站运行时的当前 url 来设置应用程序中某个类的一些静态属性。为此,我使用以下方法获取域名(即类的静态构造函数):

var host = HttpContext.Current.Request.Url.Host;

它在我的开发计算机(Windows XP / Cassini)上运行良好。但是,当我部署到 IIS7 时,出现异常:“请求在此上下文中不可用”。

我猜这是因为我在对象的静态构造函数中使用了这段代码,该代码在任何请求进入之前在 IIS 中执行;并且 Cassini 在请求发生之前不会触发静态构造函数。现在,我最初不喜欢出于这个原因从请求中提取域名的想法,但这是我找到它的唯一地方 =)

那么,有谁知道我可以获取主机域名的另一个地方?我假设 ASP .Net 必须在某种程度上独立于 HttpRequests 意识到它,我只是不知道如何访问它。

I've got an ASP .Net application running on IIS7. I'm using the current url that the site is running under to set some static properties on a class in my application. To do this, I'm getting the domain name using this (insde the class's static constructor):

var host = HttpContext.Current.Request.Url.Host;

And it works fine on my dev machine (windows XP / Cassini). However, when I deploy to IIS7, I get an exception: "Request is not available in this context".

I'm guessing this is because I'm using this code in the static constructor of an object, which is getting executed in IIS before any requests come in; and Cassini doesn't trigger the static constructor until a request happens. Now, I didn't originally like the idea of pulling the domain name from the Request for this very reason, but it was the only place I found it =)

So, does anyone know of another place that I can get the host domain name? I'm assuming that ASP .Net has got to be aware of it at some level independent of HttpRequests, I just don't know how to access it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲娇萝莉攻 2024-08-26 22:09:20

域出现在请求中的原因是……这就是所要求的。例如,这些是来自 http://www.stackexchangesites.com/ 的一些 stackexchange 站点:

如果你 ping 它们,你会看到它们都指向同一个 IP/Web 服务器,并由同一个应用程序(在本例中是多个应用程序,但如果它是一个大应用程序,则该示例成立)提供服务......但是应用程序不知道是哪一个,直到 主机标头与向服务器询问该站点的请求一起出现。每个请求可能是针对不同的域...所以应用程序不知道这一点。

但如果它没有改变,您可以将其作为 appSetting 存储在 web.config 中。

The reason that the domain is in the request is...that's what's being asked for. For example these are a few stackexchange sites from http://www.stackexchangesites.com/:

If you ping them, you'll see they all point to the same IP/Web Server and be served by the same app (or multiple apps in this case, but the example holds if it was one big one)...but the application doesn't know which one until a host header comes in with the request asking the server for that site. Each request may be to a different domain...so the application doesn't know it.

If however it doesn't change, you could store it as an appSetting in the web.config.

就像说晚安 2024-08-26 22:09:20

使用 global.asax 或编写 HttpModule 并订阅启动请求事件。您将把请求传递到您的事件处理程序中。

Use global.asax or write a HttpModule and subscribe to start request events. You will have the request passed into your event handler.

鲸落 2024-08-26 22:09:20

使用此代替:

HttpRuntime.AppDomainAppVirtualPath

或者如果您想要物理路径:

HttpRuntime.AppDomainAppPath

进一步阅读:

http://weblogs.asp.net/reganschroder/archive/2008/07/25/iis7-集成模式请求在此上下文异常中不可用。aspx

Use this instead:

HttpRuntime.AppDomainAppVirtualPath

Or if you want the physical path:

HttpRuntime.AppDomainAppPath

For further reading:

http://weblogs.asp.net/reganschroder/archive/2008/07/25/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-application-start.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文