我如何在ASP .NET中的主机域名,而无需使用HttpContext.Current.Request? [英] How do I get the host domain name in ASP .NET without using HttpContext.Current.Request?

查看:225
本文介绍了我如何在ASP .NET中的主机域名,而无需使用HttpContext.Current.Request?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有IIS7上运行的ASP .NET应用程序。我使用的是当前的URL,该网站在其下运行设定在我的应用程序在一个类中的一些静态属性。要做到这一点,我得到使用该域名此​​(insde类的静态构造函数):

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;

和它的工作原理我的开发机器(Windows XP中/卡西尼)的罚款。然而,当我部署到IIS7,我得到一个异常:请求是不是在这种情况下可用。

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".

我猜这是因为我使用的一个对象,这是在IIS中的任何请求进来之前得到执行的静态构造函数这个code;直到请求发生卡西尼不会触发静态构造函数。现在,我并没有像最初从Request出于这个原因拉动域名的想法,但它是我发现的唯一的地方吧=)

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 =)

因此​​,没有人知道另一个地方,我可以得到主机域名?我假设ASP.NET的一定是知道它在一定程度上独立的Htt prequests的,我只是不知道如何访问它。

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.

推荐答案

的原因,您所访问的请求是......那是什么东西被要求。例如这些都是 http://www.stackexchangesites.com/ 几个stackexchange网站:

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/:

  • http://community.ecoanswers.com
  • http://www.appqanda.com
  • http://www.irosetta.com/

如果您ping它们,你会看到他们都指向同一个IP / Web服务器并通过相同的应用程序送达(或多个应用程序在这种情况下,但举例持有,如果它是一个大的).. 。但应用程序不知道是哪一个,直到主机头进来与请求,要求该网站的服务器。每个请求可以是不同的域...这样的应用程序并不知道这一点。

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.

但如果不改变,你可以将其存储为在 web.config中的appSetting

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

这篇关于我如何在ASP .NET中的主机域名,而无需使用HttpContext.Current.Request?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆