Generate Full URL Path To Current Executing ASPX Excluding ASPX Filename

This will get you the full URL path to the current ASPX excluding the ASPX filename. Useful in cases where a relative path won’t work and you need the full hostname/path/.

So for example:
If this code is executing: http://www.domain.com/path/to/default.aspx –> http://www.domain.com/path/to/

// generate full URL path to aspx which does the work retrieving the TRI stock data
string StockPagePath = "http://" + HttpContext.Current.Request.Url.Host + 
Page.TemplateSourceDirectory.ToString() + "/TRIStockData.aspx";

Another example, also declaring page object

Page page = (Page)HttpContext.Current.CurrentHandler;

string errorUrl = "http://" + HttpContext.Current.Request.Url.Host + page.TemplateSourceDirectory.ToString() + "/error.aspx";

Another example including the port

string errorUrl = "http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port +  page.TemplateSourceDirectory.ToString() + "/error.aspx";
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s