[英]ASP.NET MVC FilePathResult: How to return an html file not found?
The following code is hopefully a correct way to return an image that exists on disk using ASP.NET MVC 3: 希望以下代码是使用ASP.NET MVC返回磁盘上存在的映像的正确方法:
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 ???
}
If you don't find the file, what could you return that would represent an HTML 404 error (or equivalent?) 如果找不到该文件,您将返回什么,表示HTML 404错误(或等效错误)?
You would throw new HttpException(404, "Not found");
您将
throw new HttpException(404, "Not found");
. 。 Obviously you would have a corresponding page in the
customErrors
section of your web.config to indicate the 404 to the user. 显然,您将在web.config的
customErrors
部分中具有相应的页面 ,以向用户指示404。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.