Server.MapPath returns a path with a folder that does not exist

I have the 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 the 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 NotifyMailwhen it attachmentsretrieves the path of the physical file through Server.MapPath, returning the path with the invalid folder included in it, i.e. Postthis folder does not exist anywhere, even in the hard drive, and I have no idea how it was raised and returned. But he said that because of this problem he LinkedResource(attachments);throws 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"
+1
source share
2 answers

I do not believe that MapPath guarantees that the path exists, it just binds your virtual path to the path to the context.

, ,

 HttpContext.Current.Server.MapPath

HttpContext.Current.Request.MapPath
+1

. "\\" , , "Post" ( ) .

public bool NotifyMail(string subject, string body, string toAdd, string filePath)
    {
string attachments = HttpContext.Current.Server.MapPath(@"\\" + filePath);

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

Source: https://habr.com/ru/post/1666883/


All Articles