How to determine the path to the current website

I wanted to create a function that returns the path to the current website. This is what I thought worked while working in the IDE:

Public Shared Function WebsiteAbsoluteBaseUrl() As String Dim RequestObject As System.Web.HttpRequest = HttpContext.Current.Request Return "http://" & RequestObject.Url.Host & ":" & _ RequestObject.Url.Port & "/" & _ RequestObject.Url.Segments(1) End Function 

Does this seem to work? Is there a more direct way?

I am trying to replace all occurrences of the {{WebSiteBaseUrl} tag in an html email with an absolute website address.

 <A HREF="{WebSiteBaseUrl}Files/filename.pdf/>Some Text</A> 

I do not want to parse the template and try to understand it in order to use the MapPath function regarding the relative path to the file name.

The resulting email will become the html body of the html email, so I need the links to be absolute.

 <a href="http://websitename:80/SubmitOrder.aspxFiles/filename.pdf" target="_blank">Some Text</a> 

I assume that what I see in dev running in the IDE is that "RequestObject.Url.Segments (1)" returns the name of the virtual folder, and during production it seems to return the name of the website This is likely due to the way the website is configured in IIS.

What does this say about how the website is set up for production and how do you suggest that I change the code to get the correct path after replacing {WebSiteBaseUrl} with the result of the function? This assumes that the file "filename.pdf" is in the relative path ~ / files / filesname.pdf.

+4
source share
2 answers

Try this to get the website path:

  Public shared ReadOnly Property FullyQualifiedApplicationPath() As String Get Dim appPath As String = Nothing Dim context As HttpContext = HttpContext.Current If Not context Is Nothing Then 'Formatting the fully qualified website url/name appPath = String.Format("{0}://{1}{2}{3}", _ context.Request.Url.Scheme, _ context.Request.Url.Host, _ IIf(context.Request.Url.Port = 80, String.Empty, ":" & context.Request.Url.Port), _ context.Request.ApplicationPath) End If Return appPath End Get End Property 

or try this (just founded):

 HttpContext.Current.Request.UrlInternal 
0
source

You can use Request.Url.AbsoluteUri for this. Server.MapPath(".") give you the physical path.

You can also use other Request properties, which are listed here .

+1
source

Source: https://habr.com/ru/post/1309416/


All Articles