user365268
user365268

Reputation:

get asp.net server and application url without a Request object

Is there a way to get the server url (ex: http://www.myapp.com:8080/applicationFolder) without having access to a Request object ?

I need the url at aplication_start and in some classes where the Request object with all the goodies is not available.

note: I know that getting the application folder can be done using

VirtualPathUtility.ToAbsolute("~/");

Upvotes: 3

Views: 2338

Answers (2)

Peter Huber
Peter Huber

Reputation: 3312

There can be many different addresses all pointing to the same ASP.NET website, like using IP address or name. There might be more than 1 DNS name pointing to the same ASP.NET application. Therefore, HttpApplication, the parent class of Global, does not know which URL a visitor will use. Even IIS doesn't know. Therefore, you have to wait for the first request and then check in the request what URL the visitor uses to access your site. Something like this:

string baseUrl = Context.Request.Url.GetLeftPart(UriPartial.Authority);

One has to use Context to get access to the Request during Global.Application_Start, because Global.Request is not initialised yet.

Upvotes: 1

Shoaib Shaikh
Shoaib Shaikh

Reputation: 4585

HttpContext.Current.Request is a static property that always returns the Request object currently executing for the session.

I think all you need a custom solution to know when first request is made after application starts, and then you can send any email you want.. this is the similar problem with solution here http://weblogs.asp.net/reganschroder/archive/2008/07/25/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-application-start.aspx this do first initialization check in BeginRequest event.

Upvotes: 2

Related Questions