使用PhysicalFile时如何正确捕获FileNotFoundException

时间:2019-03-13 15:20:35

标签: c# asp.net-core download

我在控制器方法中有这段代码;

try
{
    return PhysicalFile("c:\temp\my-non-existing-file.txt", "text/plain");
}
catch (FileNotFoundException)
{
    return NotFound();
}

但是,在这种情况下,catch子句不会运行,而是会向浏览器返回500 Internal Server Error。在开发人员错误页面处于活动状态时,它表明确实抛出了FileNotFoundException,但是调用堆栈表明它来自中间件。

System.IO.FileNotFoundException: Could not find file 'c:\temp\my-non-existing-file.txt'.
File name: 'c:\temp\my-non-existing-file.txt'
   at System.IO.FileInfo.get_Length()
   at Microsoft.AspNetCore.Mvc.Infrastructure.PhysicalFileResultExecutor.GetFileInfo(String path)
   at Microsoft.AspNetCore.Mvc.Infrastructure.PhysicalFileResultExecutor.ExecuteAsync(ActionContext context, PhysicalFileResult result)
   at Microsoft.AspNetCore.Mvc.PhysicalFileResult.ExecuteResultAsync(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at MyApp.Authorization.AuthorizationExtensions.<>c__DisplayClass0_0.<<---->b__1>d.MoveNext() in ---:line 51
--- End of stack trace from previous location where exception was thrown ---
   at MyApp.Authorization.AuthorizationExtensions.<>c__DisplayClass0_0.<<--->b__1>d.MoveNext() in ---:line 51
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

有人可以解释如何正确处理这种情况并改为返回404 Not Found吗?

更新:添加了完整的堆栈(带有一些名称清除功能)

1 个答案:

答案 0 :(得分:4)

正如@KirkLarkin正确指出的那样,该文件直到稍后假脱机响应时才被解析,这是在您的动作已退出之后发生的。因此,您无法在此处捕获该异常。使用自定义中间件或异常处理程序可能可以做一些事情,但是老实说,为什么不做下面的事情呢?

var filename = "c:\temp\my-non-existing-file.txt";
if (File.Exists(filename))
{
    return PhysicalFile(filename, "text/plain");
}
else
{
    return NotFound();
}

主动检查条件总是比依赖于捕获异常更好。过度依赖异常处理会降低您的应用性能。