【发布时间】:2015-10-01 03:40:43
【问题描述】:
我是 MVC 的新手,我遵循了另一个关于将本地文件加载到网页的教程,虽然它似乎适用于其他人,但我遇到了错误。
public class ImagesController : Controller
{
// GET: Images
public ActionResult SomeImage(string imageName)
{
var root = @"C:\Images\";
var path = Path.Combine(root, imageName);
path = Path.GetFullPath(path);
if (!path.StartsWith(root))
{
// Ensure that we are serving file only inside the root folder
// and block requests outside like "../web.config"
throw new HttpException(403, "Forbidden");
}
return File(path, "image/png");
}
}
我得到的错误是:
An exception of type 'System.ArgumentNullException' occurred in mscorlib.dll but was not handled in user code
Additional information: Value cannot be null.
它突出显示的行是:
var path = Path.Combine(root, imageName);
【问题讨论】:
-
imageName是否为空? -
显然
root不为空。因此imageName必须为空。你验证过imageName的值是什么吗? -
imageName 是我在视图中设置的任何内容,因此我目前正在使用:
<img class="img-responsive" src="@Url.Action("SomeImage", "Images", new { image = "Logo.png" })" alt="" /> -
你需要
new { imageName = "Logo.png" } -
@haim770 啊,菜鸟的错误,谢谢。如果您将其创建为答案,我将很乐意接受。
标签: asp.net controller asp.net-mvc-5