希望以下代码是使用ASP.NET MVC 3返回磁盘上存在的映像的正确方法:
public FilePathResult GetThumbnail(string imageName)
{
if( !String.IsNullOrEmpty(imageName) &&
Regex.IsMatch(imageName, @"^p\d{10}.jpg$"))) ) // p0000000000.jpg
{
var homePath = Server.MapPath("~/Content/previews");
var imagePath = Path.Combine( homePath, imageName );
if( System.IO.File.Exists(imagePath) )
return this.File(imagePath, "image/jpeg");
}
return ???
}
如果您没有找到该文件,您可以返回哪些代表HTML 404错误(或等效的?)
解决方法:
你会抛出新的HttpException(404,“Not found”);.显然,你的web.config的customErrors部分中有一个corresponding page,用于向用户指示404.
标签:c,asp-net-mvc-3,fileresult
来源: https://codeday.me/bug/20190518/1128873.html