Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find application root URL without using ~

Tags:

asp.net

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

like image 209
Midhat Avatar asked Apr 29 '09 11:04

Midhat


2 Answers

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

like image 188
Mark Ingram Avatar answered Sep 19 '22 22:09

Mark Ingram


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?

like image 42
Midhat Avatar answered Sep 20 '22 22:09

Midhat