tags:

views:

493

answers:

5

Hi

I need to construct the URL of a page in a String, to send it an email (as part of an email verification system). If i use the ~ symbol to denote the app root, it is taken literally.

The app will be deployed on a server on three different sites (on different ports) and each site can be accessed via 2 different URLs (one for LAn and one for internet).

So hardcoding the URL is out of question. I want to construct the url to verify.aspx in my application

Please help

A: 

You need to put the URL as part of your web application's configuration. The web application does not know how it can be reached from the outside world.

E.g. consider a scenario where there's multiple proxies and load balancers in front of your web server... how would the web server know anything but its own IP?

So, you need to configure each instance of your web application by adding the base URL e.g. as an app setting in its web.config.

mookid8000
A: 

You can use HttpRequest.RawURL (docs here)property and base your URL on that, but if you are behind any kind of redirection, the RawURL may not reflect the actual URL of your application.

pgb
+1  A: 

You need this:

HttpContext.Current.Request.ApplicationPath

It's equivalent to "~" in a URL.

http://msdn.microsoft.com/en-us/library/system.web.httprequest.applicationpath.aspx

Mark Ingram
ApplicationPath doesn't produce a URL that is usable in an email. For an email, you would need the full URL, complete with "http://.../" etc. ApplicationPath would only give you "/".
Daniel Schilling
+4  A: 

Unfortunately none of the methods listed generated the full url starting from http://---.

So i had to extract these from request.url. Something like this

Uri url=HttpContext.Current.Request.Url;
StringBuilder urlString = new StringBuilder();
urlString.Append(url.Scheme);
urlString.Append("://");
urlString.Append(url.Authority);
urlString.Append("/MyDesiredPath");

Can someone spot any potential problems with this?

Midhat
A: 

Try:

HttpRequest req = HttpContext.Current.Request;
string url = req.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.SafeUnescaped)
    + ((req.ApplicationPath.Length > 1) ? req.ApplicationPath : "");