Logo
HOWTO: Get the full browse URL of an .aspx page

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2005
Date: May 2013   Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012
Introduction

This article shows how to get programmatically (from a macro or add-in) the browse URL of an .aspx page of a Web Site or Web Application project.

More Information

.aspx pages can be viewed at design-time using the "Design" tab of the designer window. However, if you want to view them at run-time, you need either to run/debug the project, or to right-click the file in the Solution Explorer and select the "View in Browser" menu entry. If you want to show them at run-time programmatically, you can use the approach described in the article HOWTO: Open the Web Browser and navigate to a page from a Visual Studio add-in or macro, but you will need the full URL of the page. This macro shows how to compose it for Web Site projects and Web Application projects:

Language: VB.NET   Copy Code Copy Code (IE only)
    Sub MyMacro()

        Dim pi As ProjectItem
        Dim fullURL As String

        pi = DTE.Solution.Projects.Item(1).ProjectItems.Item("MyFolder").ProjectItems.Item("MyPage.aspx")

        fullURL = GetFullURL(pi)

        MessageBox.Show(fullURL)
       
    End Sub

    Function GetFullURL(ByVal pi As ProjectItem) As String

        Dim fullURL As String
        Dim baseURL As String
        Dim relativeURL As String

        baseURL = GetBaseURL(pi.ContainingProject)

        relativeURL = GetRelativeURL(pi)

        fullURL = baseURL & "/" & relativeURL

        Return fullURL

    End Function

    Function GetBaseURL(ByVal proj As Project) As String

        Dim baseURL As String
        Dim prop As EnvDTE.Property

        ' Get the base URL of the Web Site or Web Application parent project
        ' This is the case of Web Site projects
        Try
            prop = proj.Properties.Item("BrowseURL")
        Catch
        End Try

        If prop Is Nothing Then

            ' This is the case of Web Application projects
            Try
                prop = proj.Properties.Item("WebApplication.BrowseURL")
            Catch
            End Try

        End If

        If prop IsNot Nothing Then

            baseURL = prop.Value.ToString()

        End If

        Return baseURL

    End Function

    Function GetRelativeURL(ByVal pi As ProjectItem) As String

        Dim relativeURL As String
        Dim prop As EnvDTE.Property
        Dim parentProjectItem As ProjectItem

        Try
            prop = pi.Properties.Item("RelativeURL")
        Catch
        End Try

        If prop IsNot Nothing Then

            ' This is the case of Web Site projects
            relativeURL = prop.Value.ToString()

        Else

            ' This is the case of Web Application projects

            ' Compose the relative URL getting the parents (folders)
            relativeURL = pi.Name
            parentProjectItem = pi
            Do
                parentProjectItem = TryCast(parentProjectItem.Collection.Parent, ProjectItem)
                If parentProjectItem Is Nothing Then
                    Exit Do
                Else
                    relativeURL = parentProjectItem.Name & "/" & relativeURL
                End If
            Loop

        End If

        Return relativeURL

    End Function

Related articles


Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


Top