Server.MapPath problems

Discussion in 'ASP.NET / ASP.NET Core' started by soupy, Mar 3, 2009.

  1. Hi,
    I've been trying to use Server.MapPath in the following code:

    doc.Load(HttpContext.Current.Server.MapPath("xml/MailingList.xml"));

    it worked fine previously when I put the path in manually, ie:
    doc.Load("C:\\VS2008 Projects\\SLtest\\SLtest.Web\\xml\\MailingList.xml\");

    So I logged the output of the Server.MapPath to try and see what the problem was, and it seems to have the slashes removed, ie:
    C:VS2008 ProjectsSLtestSLtest.WebxmlMailingList.xml

    which would explain why it's failing to find the file.

    Does anyone know why this might be? I can't seem to find anyone who has had the same issue. I'm sure it must be simple to resolve.

    Any help much appreciated!
    John.
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    are you running this on our server or your own computer? I never heard of this problem before.
     
  3. I am now having the same problem. My code is running in ASP.Net MVC and I have created a custom ActionResult, in it I have the following overload:

    public override void ExecuteResult(ControllerContext context)
    {
    if (!String.IsNullOrEmpty(FileDownloadName))
    {
    context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + FileDownloadName);
    }

    string filePath = context.HttpContext.Server.MapPath(VirtualPath);
    context.HttpContext.Response.TransmitFile(filePath);
    }

    and this is all fine from VS 2008.
    So, I published this only to find that when I try to click the link on the host I get the following:

    Could not find a part of the path 'E:\web\<my discount.asp username>\htdocs\<virtual path>

    (paths missing for security)

    this is very urgent
     
  4. Hi John,

    Try:
    {
    doc.Load(HttpContext.Current.Server.MapPath("xml/MailingList.xml"));
    must be:
    doc.Load(HttpContext.Current.Server.MapPath(@"xml/MailingList.xml"));
    }
    Good luck

     

Share This Page