2012-05-30 159 views
2

我有一個操作,如果它在服務器上找到,應該返回一個映像。如果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 
+4

您可以發佈異常的錯誤消息和堆棧跟蹤嗎? –

+0

你有沒有在調試器中通過它來確定它確實沒有捕獲它? Exception Helper說了些什麼?可以'返回內容(「文件未找到」);'拋出一個錯誤? – Widor

+0

你怎麼知道一個異常正在被處理?也許這是在您的操作方法執行後發生的。 –

回答

3

Ť他將你正在嘗試檢索的文件不存在,所以當MVC處理你的ActionResult時你會得到一個IOException。

這發生在您的控制器方法之外,因此在您的try-catch塊之外。請嘗試使用以下代碼:

public ActionResult Users(string filename) 
{ 
    var filePath = Server.MapPath("~/App_Data/uploads/users/" + filename); 
    return File.Exists(filePath) ? File(filePath, "image/jpg", Path.GetFileName(filePath)) : Content("FILE NOT FOUND"); 
} 
+1

這工作,謝謝。即使我仍然不明白爲什麼我的catch塊不起作用。 : -/ – android

+1

您的控制器操作不會返回Web請求的實際結果。相反,它返回MVC用來生成將發送給客戶端的結果的信息。在你的情況下,控制器只返回文件名和內容類型。在你的控制器方法返回之後,這個信息被MVC用來加載文件,但由於實際的文件檢索是由MVC完成的,而不是由你的代碼完成的,你的異常處理程序不會捕獲錯誤。 –

+0

感謝您的解釋,我現在明白了。 :-) – android