ASP.NET MVC FilePathResult:如何返回未找到的 html 文件?
以下代码希望是使用 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 错误(或等效错误?)
The following code is hopefully a correct way to return an image that exists on disk using 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 ???
}
If you don't find the file, what could you return that would represent an HTML 404 error (or equivalent?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将
抛出 new HttpException(404, "Not found");
。显然,您的 web.config 的customErrors
部分中会有一个相应页面,以向用户指示 404 。You would
throw new HttpException(404, "Not found");
. Obviously you would have a corresponding page in thecustomErrors
section of your web.config to indicate the 404 to the user.