Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Missing - WebControl.MapPath()

Missing - WebControl.MapPath()

Scheduled Pinned Locked Moved ASP.NET
csharpdesignsysadminxmlhelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steve Murrell
    wrote on last edited by
    #1

    Hi, I am writing a Web Control and have a problem at design time. There is no WebControl.MapPath() function. I am loading an XML file which cannot be done using relative path names. At design time you cannot access Page.MapPath() as no page exists. I have a horrid solution where you specify the full path and filename at design time and then mangle it at runtime, but I hoping someone could come up with something better than this - String MapXMLFileName(String xmlFileName) { //When you specify the XML Filename at design time you select //the absolute path and filename on the development server //(otherwise it can't be loaded at design time). //At run time however this now needs to be converted into a mappped path. //We need to allow for subdirectory differences e.g - // //xmlFileName = "c:\inetpub\wwwroot\dotNet\TestApp\XML\Menu1.xml" //but should be = "d:\intepub\wwwroot\companyname\TestApp\XML\Menu1.xml" // //Map a dummy name first - "./Test.xml" becomes //xmlDummy = "d:\intepub\wwwroot\companyname\TestApp\Test.xml" // //Next strip off the dummy filename so we get //xmlMappedRoot = "d:\intepub\wwwroot\companyname\TestApp" // //Now extract the directory we are working from //xmlDirectorySegment = "TestApp" // //Now look in the original xmlFileName to find that Directory and take //everything after that //xmlPathSegment = "\XML\Menu1.xml" // //Finally add the xmlMappedRoot and the xmlPathSegment to get the //new mapped path //xmlMapped = "d:\intepub\wwwroot\companyname\TestApp\XML\Menu1.xml" String xmlDummy = Page.MapPath("./Test.xml"); String xmlMappedRoot = xmlDummy.Substring(0,xmlDummy.Length - 9); String xmlDirectorySegment = xmlMappedRoot.Substring(xmlMappedRoot.LastIndexOf("\\")+1); String xmlPathSegment = xmlFileName.Substring(xmlFileName.LastIndexOf(xmlDirectorySegment)+xmlDirectorySegment.Length); String xmlMapped = xmlMappedRoot + xmlPathSegment; return xmlMapped; } Cheers Steve

    P P 2 Replies Last reply
    0
    • S Steve Murrell

      Hi, I am writing a Web Control and have a problem at design time. There is no WebControl.MapPath() function. I am loading an XML file which cannot be done using relative path names. At design time you cannot access Page.MapPath() as no page exists. I have a horrid solution where you specify the full path and filename at design time and then mangle it at runtime, but I hoping someone could come up with something better than this - String MapXMLFileName(String xmlFileName) { //When you specify the XML Filename at design time you select //the absolute path and filename on the development server //(otherwise it can't be loaded at design time). //At run time however this now needs to be converted into a mappped path. //We need to allow for subdirectory differences e.g - // //xmlFileName = "c:\inetpub\wwwroot\dotNet\TestApp\XML\Menu1.xml" //but should be = "d:\intepub\wwwroot\companyname\TestApp\XML\Menu1.xml" // //Map a dummy name first - "./Test.xml" becomes //xmlDummy = "d:\intepub\wwwroot\companyname\TestApp\Test.xml" // //Next strip off the dummy filename so we get //xmlMappedRoot = "d:\intepub\wwwroot\companyname\TestApp" // //Now extract the directory we are working from //xmlDirectorySegment = "TestApp" // //Now look in the original xmlFileName to find that Directory and take //everything after that //xmlPathSegment = "\XML\Menu1.xml" // //Finally add the xmlMappedRoot and the xmlPathSegment to get the //new mapped path //xmlMapped = "d:\intepub\wwwroot\companyname\TestApp\XML\Menu1.xml" String xmlDummy = Page.MapPath("./Test.xml"); String xmlMappedRoot = xmlDummy.Substring(0,xmlDummy.Length - 9); String xmlDirectorySegment = xmlMappedRoot.Substring(xmlMappedRoot.LastIndexOf("\\")+1); String xmlPathSegment = xmlFileName.Substring(xmlFileName.LastIndexOf(xmlDirectorySegment)+xmlDirectorySegment.Length); String xmlMapped = xmlMappedRoot + xmlPathSegment; return xmlMapped; } Cheers Steve

      P Offline
      P Offline
      Philip Patrick
      wrote on last edited by
      #2

      Not sure it helps, but what about System.Web.HttpContext.Current.Server.MapPath? Philip Patrick Web-site: www.stpworks.com "Two beer or not two beer?" Shakesbeer

      S 1 Reply Last reply
      0
      • P Philip Patrick

        Not sure it helps, but what about System.Web.HttpContext.Current.Server.MapPath? Philip Patrick Web-site: www.stpworks.com "Two beer or not two beer?" Shakesbeer

        S Offline
        S Offline
        Steve Murrell
        wrote on last edited by
        #3

        No Luck - Server.MapPath also is not valid at design time. Cheers Steve

        1 Reply Last reply
        0
        • S Steve Murrell

          Hi, I am writing a Web Control and have a problem at design time. There is no WebControl.MapPath() function. I am loading an XML file which cannot be done using relative path names. At design time you cannot access Page.MapPath() as no page exists. I have a horrid solution where you specify the full path and filename at design time and then mangle it at runtime, but I hoping someone could come up with something better than this - String MapXMLFileName(String xmlFileName) { //When you specify the XML Filename at design time you select //the absolute path and filename on the development server //(otherwise it can't be loaded at design time). //At run time however this now needs to be converted into a mappped path. //We need to allow for subdirectory differences e.g - // //xmlFileName = "c:\inetpub\wwwroot\dotNet\TestApp\XML\Menu1.xml" //but should be = "d:\intepub\wwwroot\companyname\TestApp\XML\Menu1.xml" // //Map a dummy name first - "./Test.xml" becomes //xmlDummy = "d:\intepub\wwwroot\companyname\TestApp\Test.xml" // //Next strip off the dummy filename so we get //xmlMappedRoot = "d:\intepub\wwwroot\companyname\TestApp" // //Now extract the directory we are working from //xmlDirectorySegment = "TestApp" // //Now look in the original xmlFileName to find that Directory and take //everything after that //xmlPathSegment = "\XML\Menu1.xml" // //Finally add the xmlMappedRoot and the xmlPathSegment to get the //new mapped path //xmlMapped = "d:\intepub\wwwroot\companyname\TestApp\XML\Menu1.xml" String xmlDummy = Page.MapPath("./Test.xml"); String xmlMappedRoot = xmlDummy.Substring(0,xmlDummy.Length - 9); String xmlDirectorySegment = xmlMappedRoot.Substring(xmlMappedRoot.LastIndexOf("\\")+1); String xmlPathSegment = xmlFileName.Substring(xmlFileName.LastIndexOf(xmlDirectorySegment)+xmlDirectorySegment.Length); String xmlMapped = xmlMappedRoot + xmlPathSegment; return xmlMapped; } Cheers Steve

          P Offline
          P Offline
          Paul Watson
          wrote on last edited by
          #4

          The other chap is correct, you must use Context to get at Server.MapPath You go Context.Server.MapPath. If that does not work then what is your WebControl inheriting from? It should be public class WebCustomControl1 : System.Web.UI.WebControls.WebControl or similar

          Paul Watson
          Bluegrass
          Cape Town, South Africa

          brianwelsch wrote: I find my day goes by more smoothly if I never question other peoples fantasies. My own disturb me enough.

          S 1 Reply Last reply
          0
          • P Paul Watson

            The other chap is correct, you must use Context to get at Server.MapPath You go Context.Server.MapPath. If that does not work then what is your WebControl inheriting from? It should be public class WebCustomControl1 : System.Web.UI.WebControls.WebControl or similar

            Paul Watson
            Bluegrass
            Cape Town, South Africa

            brianwelsch wrote: I find my day goes by more smoothly if I never question other peoples fantasies. My own disturb me enough.

            S Offline
            S Offline
            Steve Murrell
            wrote on last edited by
            #5

            Paul, I am inheriting from System.Web.UI.WebControls.WebControl. Server.MapPath is fine at runtime, but when I use it from my custom designer which inherits from ControlDesigner it blows up in GetDesignTimeHtml() as no Context is available. When I drag the control from the toolbox and drop it, I get Grey box saying Error Creating Control. As soon as I remove the call to Server.MapPath I can use my control again at design time. I guess whatever is hosting the control at design time is not supplying the required interfaces. It's a bit of a pain as my control is an XML driven menu and really needs to load the XML file at design time in order to look the part of a nice WYSIWYG control. Cheers Steve

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups