Server.MapPath返回带有不存在的文件夹的路径 [英] Server.MapPath returning a path with a folder that doesn’t exists

查看:49
本文介绍了Server.MapPath返回带有不存在的文件夹的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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);

与NotifyMail函数相比,我有这个:

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";
…
    }

现在问题出在 NotifyMail 中,当附件通过 Server.MapPath 检索物理文件路径时,它返回包含无效文件夹的路径在其中,即 Post ,该文件夹在任何地方都不存在,甚至在硬盘驱动器中也不存在,而且我也不知道如何将其拾起并返回.但这是由于此问题导致的 LinkedResource(attachments); 引发异常:

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"’

推荐答案

我不认为MapPath保证存在路径,它只是将虚拟路径添加到上下文路径.

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

尝试使用

HttpContext.Current.Request.MapPath

这篇关于Server.MapPath返回带有不存在的文件夹的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆