我有一個操作,如果它在服務器上找到,應該返回一個映像。如果filename
有效,則所有內容都按預期工作。但是如果文件沒有找到,它總是會拋出一個未處理的異常:捕獲塊沒有發現異常
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.
爲什麼這個異常不被catch塊處理?
這是我的簡化的代碼:
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.
堆棧跟蹤:
[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
您可以發佈異常的錯誤消息和堆棧跟蹤嗎? –
你有沒有在調試器中通過它來確定它確實沒有捕獲它? Exception Helper說了些什麼?可以'返回內容(「文件未找到」);'拋出一個錯誤? – Widor
你怎麼知道一個異常正在被處理?也許這是在您的操作方法執行後發生的。 –