找不到文件时处理 FileContentResult
我有一个控制器操作,它根据容器引用名称(即 blob 中文件的完整路径名)从 azure blob 下载文件。代码如下所示:
public FileContentResult GetDocument(String pathName)
{
try
{
Byte[] buffer = BlobStorage.DownloadFile(pathName);
FileContentResult result = new FileContentResult(buffer, "PDF");
String[] folders = pathName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
// get the last one as actual "file name" based on some convention
result.FileDownloadName = folders[folders.Length - 1];
return result;
}
catch (Exception ex)
{
// log error
}
// how to handle if file is not found?
return new FileContentResult(new byte[] { }, "PDF");
}
BlobStorage 类是我的帮助程序类,用于从 blob 下载流。
我的问题在代码注释中陈述:当找不到文件/流时,我应该如何处理场景?目前,我正在传递一个空的 PDF 文件,我认为这不是最好的方法。
I have a controller action that downloads a file from an azure blob based on the container reference name (i.e. full path name of the file in the blob). The code looks something like this:
public FileContentResult GetDocument(String pathName)
{
try
{
Byte[] buffer = BlobStorage.DownloadFile(pathName);
FileContentResult result = new FileContentResult(buffer, "PDF");
String[] folders = pathName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
// get the last one as actual "file name" based on some convention
result.FileDownloadName = folders[folders.Length - 1];
return result;
}
catch (Exception ex)
{
// log error
}
// how to handle if file is not found?
return new FileContentResult(new byte[] { }, "PDF");
}
The BlobStorage
class there is my helper class to download the stream from the blob.
My question is stated in the code comment: How should I handle the scenario when the file/stream is not found? Currently, I am passing an empty PDF file, which I feel is not the best way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
处理 Web 应用程序中未找到的正确方法是向客户端返回 404 HTTP 状态代码,在 ASP.NET MVC 术语中,该状态代码转换为返回 HttpNotFoundResult 来自您的控制器操作:
啊,哎呀,没有注意到您仍在 ASP.NET MVC 2 上。您可以实现它因为
HttpNotFoundResult
仅在 ASP.NET MVC 3 中引入:The correct way to handle a not found in a web application is by returning a 404 HTTP status code to the client which in ASP.NET MVC terms translates into returning a HttpNotFoundResult from your controller action:
Ahh, oops, didn't notice you were still on ASP.NET MVC 2. You could implement it yourself because
HttpNotFoundResult
was introduced only in ASP.NET MVC 3:在 ASP.NET Core 中,使用
NotFound()
您的控制器必须继承
Controller
并且该方法必须返回ActionResult
示例:
注意:上面的代码不会处理所有文件操作错误的情况,如路径中的无效字符或文件不可用(因为它超出了当前答案的范围)和正确的响应(例如由于权限或限制文件访问否则)
In ASP.NET Core, use
NotFound()
Your controller must inherit of
Controller
and the method must returnActionResult
Example:
Note: the above code doesn't handle all cases of file operations errors as invalid chars in path or file unavailability (because it is beyond the scope of the current answer) and proper responses (like restricted file access due to permissions or else)
使用 ASP.NET Core 中的 CQRS 模式,您可以返回 NoContentResult。
Using the CQRS pattern in ASP.NET Core, you can return NoContentResult.