У меня есть действие, которое должно вернуть изображение, если оно было обнаружено на сервере. Если filename
действительно, все работает так, как ожидалось. Но если файл не был найден, он всегда выдает необработанное исключение:Catch block is not catch exception
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Почему это исключение не обрабатывается блоком поймать?
Это мой упрощенный код:
public ActionResult Users(string filename)
{
try
{
var filePath = Server.MapPath("~/App_Data/uploads/users/" + filename);
var file = File(filePath, "image/jpg", Path.GetFileName(filePath));
return file;
}
catch
{
return Content("FILE NOT FOUND");
}
}
EDIT
Ошибка:
Could not find file 'C:\Projects\Extranet\App_Data\uploads\users\test123.png'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\Projects\Extranet\App_Data\uploads\users\test123.png'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
StackTrace:
[FileNotFoundException: Could not find file 'C:\Projects\Extranet\App_Data\uploads\users\test123.png'.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +9726591
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) +1142
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +83
System.Web.HttpResponse.TransmitFile(String filename, Int64 offset, Int64 length) +132
System.Web.HttpResponseWrapper.TransmitFile(String filename) +20
System.Web.Mvc.FilePathResult.WriteFile(HttpResponseBase response) +17
System.Web.Mvc.FileResult.ExecuteResult(ControllerContext context) +188
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8967885
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
Можете ли вы опубликовать сообщение об ошибке и stacktrace исключения? –
Вы прошли через него в отладчике, чтобы убедиться, что это действительно не ловить его? Что сказал Помощник Исключения? Может ли «вернуть контент» («ФАЙЛ НЕ НАЙДЕН»), «выпустить ошибку? – Widor
Как вы знаете, что исключение не обрабатывается? Возможно, это происходит после выполнения вашего действия. –