Server.MapPath("/") in 2.0

Posted on 2007-12-06
Last Modified: 2010-04-21
in 2.0 I am trying to use the Server.MapPath("/") to return the root directory of the application...

it errors with:
Exception Details: System.InvalidOperationException: Failed to map the path '/'.
Label1.Text = "Mail sent: " & Server.MapPath("/")

what am I doing wrong? thanks!
Question by:BobCSD
LVL 29

Accepted Solution

Göran Andersson earned 400 total points
ID: 20423947
That's a bit surprising.

Anyhow, the path "/" doesn't refer to the root directory of the application, but the root directory of the web site. Use "~/" to get the root directory of the application.

Assisted Solution

nisarkhan earned 100 total points
ID: 20424013
RootUrl - the fully qualified path to the page that should serve as the root node in the site map. If this value isn't specified, the default value is ~/Default.aspx.

see my sample in code snippet:

IssueManagement is the app name/virtual directory. database is the directory under it.
This gets mapped to C:\Inetpub\wwwroot\IssueManagement\database\Issues.mdb

Hope this helps
Public Overrides Function BuildSiteMap() As System.Web.SiteMapNode

    'Need to lock to ensure thread safety, since multiple pages in the app

    'might be trying to call this method concurrently

    SyncLock Me

        'See if a root has been defined

        If _root IsNot Nothing Then

            'We have a root - but has the underlying file system been changed?

            If Not _fsMonitor.HasChanged Then

                'No change to FS, returned the cached root

                Return _root

            End If

            'The file system has been changed since we've last called

            'BuildSiteMap - we need to rebuild the sitemap

        End If

        'If we reach here, either we don't have a root or the file system has

        'been changed must build up the Site Map. Clear out the site map, if 

        'it already exists...


        'Create a root node

        _root = CreateFolderNode(HttpContext.Current.Server.MapPath(RootUrl), RootUrl)

        _root.Title = RootTitle

        'Establish the cache dependency

        _fsMonitor = New CacheDependency(HttpContext.Current.Server.MapPath("~/"))

        AddNode(_root)  'Add the root to the site map

        'Recurse through the file system, adding nodes

        BuildSiteMapFromFileSystem(_root, "~/")

        Return _root

    End SyncLock

End Function

Open in new window


Author Closing Comment

ID: 31413274
thanks guys!

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now