Asp.net Url path and absolute path

There are quiet few cases that we need to find the file absolute path on the storage and the url path which can be referenced. The following is the the summary.

The test.pdf file is located at “c:\Inetpub\VirtualWebsite\Subfolderpath\test.pdf”
The url should be: http://servername/VirtualWebsite/Subfolderpath/test.pdf

 'http://servername/VirtualWebsite/Subfolderpath/default.aspx
 Dim strPathAndQuery As String = HttpContext.Current.Request.Url.PathAndQuery

 'http://servername/
 Dim strUrl As String = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/")

 'http:// or https://
 Dim protocolpath As String = HttpContext.Current.Request.Url.Scheme + "://"

 'servername or domainname
 Dim apphost As String = HttpContext.Current.Request.Url.Authority

 '/subfolder
 Dim apppath As String = HttpContext.Current.Request.ApplicationPath

 'the whold url path
 pdffilepath = protocolpath + apphost + apppath + "subfolderpath/test.pdf"  

'"c:\Inetpub\VirtualWebsite\Subfolderpath\test.pdf"
 Server.MapPath("~\downloads\") + filename

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