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: –>

// 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";

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s