确定不使用HttpContext.Current的URL主机名? [英] Determine the URL hostname without using HttpContext.Current?

查看:330
本文介绍了确定不使用HttpContext.Current的URL主机名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用当前请求我可以获取URL主机名:

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

HttpContext.Current.Request.Url.Host

可是 - 我需要确定URL主机名不使用当前请求( HttpContext.Current )。这样做的原因是,我的code从的SqlDependency 为onChange回调调用时找到一个SQL的依赖性。 Althougth的code所在我的Web应用程序,也没有要求,而 HttpContext.Current 为null。

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 SqlDependency in the onChange callback for when a SQL Dependency is found. Althougth the code resides in my web app, there is no request, and HttpContext.Current is null.

我希望我可以从的httpRuntime 抓住它,但似乎并没有被使用岂有。有没有一种方法可以让我得到这个信息?

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?

推荐答案

如果你知道你设置了事件处理的那一刻主机,那么你应该能够做到像(code没有实际测试):

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);
});

这篇关于确定不使用HttpContext.Current的URL主机名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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