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

2

1

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, "[email protected]", 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"’
Chymotrypsin answered 7/8, 2013 at 13:50 Comment(0)
W
1

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
Whitman answered 7/8, 2013 at 14:57 Comment(2)
tried but still that extra unknown folder in the returned path.Chymotrypsin
Anything in the web.config or IIS application settings that mentions "Post"? My guess is it was a typo of Posts perhaps..??? Also, probably make no difference but @"Content\Posts\" + yr + @"\" + mnth + @"\" should use / and not \, since it's a relative URL, not a filepath.Whitman
G
0

I had a similar issue, You just need to add an extra "\\" double backslash before your filepath, like below and the extra "Post" word (which is your class name) will be gone.

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";
    }
Gilligan answered 11/6, 2017 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.