Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a web application's URI during startup

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.

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.

like image 227
Gabriel S. Avatar asked Jun 07 '13 14:06

Gabriel S.


People also ask

How do I launch a URI from an app?

Call LaunchUriAsync to launch a URI. Use the LaunchUriAsync method to launch a URI. When you call this method, your app must be the foreground app, that is, it must be visible to the user. This requirement helps ensure that the user remains in control.

How to launch the default app for a URI scheme?

Launch the default app for a URI. 1 Call app URI scheme. Use the ms-call: URI scheme to launch the Call app. Calls app settings page. 2 Email URI scheme. 3 HTTP URI scheme. 4 Maps app URI schemes. 5 Messaging app URI scheme. More items

What are the requirements for the Uri launch method?

When you call this method, your app must be the foreground app, that is, it must be visible to the user. This requirement helps ensure that the user remains in control. To meet this requirement, make sure that you tie all URI launches directly to the UI of your app. The user must always take some action to initiate a URI launch.

What is the use of startupuri in Windows?

Application. Startup Uri Property System. Windows Gets or sets a UI that is automatically shown when an application starts. A Uri that refers to the UI that automatically opens when an application starts. StartupUri is set with a value of null. You can use StartupUri to automatically load a UI resource when an application starts.


1 Answers

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"
  • SiteName => "Default Web Site"

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.

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?

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.

like image 53
Matt Houser Avatar answered Oct 05 '22 22:10

Matt Houser