tags:

views:

65

answers:

2

I wanted to create a function which would return the path of the current web site. This is what I thought was working while running 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

this seem like it should work? Is there a more straight forward way?

I am trying to replace all occurrences of the tag "{WebSiteBaseUrl} in an html email with the absolute web site path.

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

I don't want to have to parse the template and try to make sense of it in order to use MapPath function on a relative path to a file name.

The resulting email will become the html body of an 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 guess what I am seeing in dev, running in the IDE, is that "RequestObject.Url.Segments(1)" returns the name of the virtual folder and that in production it seems to be returning the name of the web page. I guess this probably has to do with how the web site is set up in IIS.

What does this suggest about how the web site is set up in production and how would you suggest I modify the code to yield a correct path after replacing {WebSiteBaseUrl} with the result of the function? This assumes that the file "filename.pdf" is in the relativepath ~/files/filesname.pdf.

+1  A: 

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

Also you can use other Request properties which are listed here.

sashaeve
+1  A: 

Try this to get the website's application 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
Tim Schmelter
HttpContext.Current.Request.UrlInternal is a good suggestion, but I need to parse off the web file name. I just need the top level folder. Any suggestions, or should I parse? Also, note my edited question.
Velika2
What returns my 'FullyQualifiedApplicationPath'-property?
Tim Schmelter
Your code looks promising. Let me try it out in a liev site and I'll let you know and then select this as the answer.
Velika2