.netasp.net-mvcserver.mappath

Server.MapPath returning a path with a folder that doesn’t exists


I have following code:

var dir = @"Content\Posts\" + yr + @"\" + mnth + @"\";
var a = Path.Combine(dir, dy.ToString() + pId.ToString() + ".txt");
//a contains: "Content\\Posts\\2013\\8\\file01.txt"
stts = obj.NotifyMail(title, writeup, "author@gmail.com", a);

And than in NotifyMail function I have this:

public bool NotifyMail(string subject, string body, string toAdd, string filePath)
    {
        …
string attachments = HttpContext.Current.Server.MapPath(filePath);
//NOW here attachments contains: "G:\\Program Files\\Derby\\Work\\Development\\proj\\proj\\`Post`\\Content\\Posts\\2013\\8\\file01.txt"

            var attchmnts = new LinkedResource(attachments);
            attchmnts.ContentId = "attchmnts";
…
    }

Now the problem is in NotifyMail when attachments is retrieving physical file path through Server.MapPath its returning a path with an invalid folder included in it i.e. Post this folder doesn't exists anywhere, not even in the hard drive and I have no idea how it is been picked up and returned. But said this due to this problem LinkedResource(attachments); is throwing an exception:

{"Could not find a part of the path ‘G:\\Program Files\\Derby\\Work\\Development\\proj\\proj\\Post\\Content\\Posts\\2013\\8\\file01.txt"’

Solution

  • I don't believe MapPath guarantees that a path exists, it just tacks your virtual path to the context path.

    I think your problem is that you're using

     HttpContext.Current.Server.MapPath
    

    try using

    HttpContext.Current.Request.MapPath