【发布时间】:2011-05-13 16:44:45
【问题描述】:
我试过了
Uri uri = HttpContext.Current.Request.Url;
String host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;
它在我的本地机器上运行良好,但是在发布到 IIS7 时,出现异常提示
System.Web.HttpException: Request is not available in this context
有人知道如何实现吗?
【问题讨论】:
-
听起来你是在集成模式下运行,而不是经典模式(我认为你在本地运行)。问题是 IIS7 的 Application_Start 中的 Request 不可用。看看:mvolo.com/blogs/serverside/archive/2007/11/10/… 解释得很好。
-
另请参阅stackoverflow.com/a/578083/12484 以获得从 URL 获取方案 + 主机 + 端口的更简洁的方法(一旦你有一个)。
标签: c# asp.net url request hostname