在启动过程中获取Web应用程序的URI [英] Getting a web application's URI during startup

查看:99
本文介绍了在启动过程中获取Web应用程序的URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找出在IIS 7.5+托管,应用程序的启动权后的ASP.NET Web API应用程序的基本URI,但任何客户端请求之前可能已经达到了。在这里我需要这个场景是:定期进行检查,通过运行独立的用户请求和与该应用程序启动(同样的过程)一起触发一个计时器;如果检查通过一定的条件下,一些注册用户将收到包含超链接到我的web应用程序的电子邮件。现在,我不想硬code链接的任何地方,但是从Web应用程序本身,而得到它的动态。这将是简单的从客户端请求的上下文中搞清楚,然后缓存在内存中,但是,你可以想像,任何请求到达服务器之前,计时器会熄灭。

I need to find out the base URI of an ASP.NET Web API application hosted in IIS 7.5+, right after the app's startup, but before any client request may have reached it. The scenario where I need this is the following: a periodic check is performed, through a timer that runs independent of user requests and which is triggered together with the app startup (same process); if this check passes certain conditions, some registered users will receive an email containing a hyperlink to my web application. Now, I don't want to hardcode that link anywhere, but rather get it dynamically from the web application itself. It would be simple to figure it out from inside the context of a client request and then cache it in memory but, as you can imagine, the timer might go off before any request reaches the server.

我怎么能那么准确地确定应用程序的基本URI?我想最合适的地方将是的Global.asax.cs 文件,Web应用程序的启动过程中,但我无法找到任何疑似有帮助的。

How could I then determine correctly the application's base URI? I was thinking the most appropriate place would be the Global.asax.cs file, during the web app's startup, yet i couldn't find anything that looked helpful.

推荐答案

给出一个完整的URL,如 HTTP ://mydomain.com/MyApplication/Controller/Action ,你可以得到一些从 System.Web.Hosting.HostingEnvironment.ApplicationHost 对象信息。你可以得到以下几点:

Given a full URL such as "http://mydomain.com/MyApplication/Controller/Action", you can get some of this information from the System.Web.Hosting.HostingEnvironment.ApplicationHost object. You can get the following:


  • ApplicationVirtualPath - >/ MyApplication的

  • 站点名称=>默认Web站点

但是,您将无法获得该域名的实际请求到来之前,这是因为一个IIS网站可以接受许多不同领域,其中一些通过DNS是唯一已知的,从来没有在任何地方配置的请求。

However, you will not be able to get the domain name before an actual request comes in. This is because an IIS website can accept requests for many different domains, some of which are only known via DNS and never configured anywhere.

例如,你的网站可以既 mydomain.com www.mydomain.com 回应。这是你希望把你的链接正确的?

For example, your website could respond to both mydomain.com and www.mydomain.com. Which is the correct one that you want to put in your link?

您可以配置IIS网站仅接受请求特定的主机连接,但不能从ASP.NET应用程序中检索。

You can configure your IIS website to only accept connections that request a particular host, but that cannot be retrieved from an ASP.NET application.

这篇关于在启动过程中获取Web应用程序的URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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