【发布时间】:2014-03-11 10:50:01
【问题描述】:
我创建了一个名为 MyLongRunningClass 的类,其中包含以下方法:
public string ProcessLongRunningAction(IEnumerable<HttpPostedFileBase> files ,string id)
{
int currentIndex = 0;
foreach (HttpPostedFileBase file in files)
{
currentIndex++;
lock(syncRoot)
{
string path=HttpContext.Current.Server.MapPath("~//Content//images //"+file.FileName);//Excecption is created here........
file.SaveAs(path);
}
Thread.Sleep(300);
}
return id;
}
从控制器调用此方法,并使用要保存在图像目录中的文件列表。每当HttpContext.Current.Server.MapPath("~//Content//images//"+file.FileName) 被执行时NullReferenceException 被抛出,HttpContext.Current 总是null。当我使用会话时也会发生同样的事情。我不知道代码有什么问题。
【问题讨论】:
-
将目标路径传递给方法,有助于关注点分离。
标签: asp.net-mvc