C# 在不使用 HttpContext.Current 的情况下确定 URL 主机名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8834677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:43:25  来源:igfitidea点击:

Determine the URL hostname without using HttpContext.Current?

c#asp.nethttpcontext

提问by Matt Roberts

Using the current request I can get the URL hostname with:

使用当前请求,我可以通过以下方式获取 URL 主机名:

HttpContext.Current.Request.Url.Host

But - I need to determine the URL hostname without using the current request (HttpContext.Current). The reason for this is that my code is called from a SqlDependencyin the onChange callback for when a SQL Dependency is found. Althougth the code resides in my web app, there is no request, and HttpContext.Currentis null.

但是 - 我需要在不使用当前请求 ( HttpContext.Current) 的情况下确定 URL 主机名。这样做的原因是SqlDependency当找到 SQL 依赖项时,我的代码是从onChange 回调中的 a 调用的。虽然代码驻留在我的网络应用程序中,但没有请求,并且HttpContext.Current为空。

I was hoping I could grab it from HttpRuntime, but there doesn't seem to be anything of use there. is there a way I can get this information?

我希望我能从 中抓住它HttpRuntime,但那里似乎没有任何用处。有什么方法可以获得这些信息吗?

采纳答案by Journey

If you know the host at the moment you're setting up the event handler then you should be able to do something like (code not actually tested):

如果您在设置事件处理程序时知道主机,那么您应该能够执行以下操作(代码未经实际测试):

string host = HttpContext.Current.Request.Url.Host;
var dep = new SqlDependency(cmd);
dep.OnChange += ((sender, args) =>
{
    DoStuff(host);
});

回答by Wiktor Zychla

How about

怎么样

Environment.MachineName

回答by Thorsten Hans

You should use the IIS api to query the information from the website you're looking for. Because depending on the IIS configuration your URL or Hostname could be differing. (Think about hostheaders, ports, protocols and stuff like this.

您应该使用 IIS api 从您要查找的网站查询信息。因为根据 IIS 配置,您的 URL 或主机名可能会有所不同。(想想主机头、端口、协议和类似的东西。

A introduction for IIS API could be found at http://learn.iis.net/page.aspx/165/how-to-use-microsoftwebadministration/

可以在http://learn.iis.net/page.aspx/165/how-to-use-microsoftwebadministration/找到 IIS API 的介绍

回答by Joel Etherton

If you are running this from a web application, and it is all managed code then HttpContext must exist. Does your child library (assuming your managed code is in a library) have a reference to System.Web? If not, consider adding this reference. From that point you should be able to access the HttpContext directly by using the fully qualified namespace:

如果您从 Web 应用程序运行它,并且它都是托管代码,则 HttpContext 必须存在。您的子库(假设您的托管代码在库中)是否引用了 System.Web?如果没有,请考虑添加此引用。从那时起,您应该能够使用完全限定的命名空间直接访问 HttpContext:

System.Web.HttpContext.Current.Request.Url.Host

In any case, unless your code is unmanaged or your context truly does not originate with a web application, HttpContext should be available at every point while the thread is alive.

在任何情况下,除非您的代码是非托管的或者您的上下文确实不是源自 Web 应用程序,否则 HttpContext 应该在线程处于活动状态时的每个点都可用。

Edit:
Based on reading your comment below, it sounds like the SqlDependency is being fired independently. While it's on the same thread, it's not being fired directly by the request. Since all you're looking for is the host url, it's not inconceivable that you can create an application variable or a static variable to hold this information in the event that it is needed for a dependency.

编辑:
根据阅读您在下面的评论,听起来 SqlDependency 是独立触发的。虽然它在同一个线程上,但它不会被请求直接触发。由于您要查找的只是主机 url,因此您可以创建应用程序变量或静态变量来保存此信息,以防万一依赖项需要这些信息,这并非不可想象。

Also something I have seen is that while HttpContext.Currentmay not be available, HttpContext.Requestmight be. These should be the same object, but they may not necessarily be. It's possible that the Host may be found there.

另外我看到的是,虽然HttpContext.Current可能不可用,但HttpContext.Request可能是。这些应该是同一个对象,但不一定是。有可能在那里找到主机。