2015-04-23 18 views
2

我剛開始收到「文件不存在」異常。我在Visual Studio中按下運行按鈕後發生異常。據我所知,也沒有我的代碼被擊中。應該打的代碼部分是ASP.NET MVC中的「文件不存在」

public ActionResult Index() { 
    return View("Index"); 
} 

但是該代碼中的斷點從未被命中。 ..我跟着How to solve exception "File does not exist"?System.Web.HttpException File does not exist - Page loads just fine (ASP.NET) 並寫了下面的代碼:「文件不存在http://localhost:63456/

void Application_Error(object sender, EventArgs e) 
{ 
    Exception ex = Server.GetLastError(); 

    if (ex.Message == "File does not exist.") 
    { 
     var foo = Request.RawUrl; 
     var newEx = new Exception(string.Format("{0} {1}", ex.Message, HttpContext.Current.Request.Url.ToString()), ex); 
    } 
} 

現在,當我跑打這個代碼,FOO被設置爲「/」和newEX是

任何想法?

這裏是堆棧跟蹤:

PishiWebSite.dll PishiWebSite.MvcApplication.Application_Error(對象發件人,發送System.EventArgs)線164 C# System.Web.dll中的System.Web!。 HttpApplication.RaiseOnError()+ 0xc6 bytes
System.Web.dll!System.Web.HttpApplication.RecordError(System.Exception error)+ 0x40 bytes
System.Web.dll!System.Web.HttpApplication.ApplicationStepManager.ResumeSteps( System.Exception錯誤)+ 0x101字節
系統。 Web.dll!System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(System.Web.HttpContext context,System.AsyncCallback cb,object extraData)+ 0xf8 bytes
System.Web.dll!System.Web.HttpRuntime。 ProcessRequestInternal(System.Web.HttpWorkerRequest wr)+ 0x284 bytes System.Web.dll!System.Web.HttpRuntime.ProcessRequestNoDemand(System.Web.HttpWorkerRequest wr)+ 0x6e bytes
System.Web.dll!System.Web.HttpRuntime .ProcessRequest(System.Web.HttpWorkerRequest wr)+ 0x47 bytes
WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Request.Process()+ 0x188 bytes WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Host。 ProcessRequest(Microsoft.VisualStudio.WebHost.Connection conn)+ 0x66字節
[應用程序域過渡]
WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Server.OnSocketAccept(對象acceptedSocket)+ 0xa1字節
mscorlib.dll中!System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(對象狀態)+ 0x3E的字節 mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state,bool preserveSyncCtx)+ 0xa7 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System .Threading.ExecutionContext executionContext,System.Threading.ContextCallback回調,對象狀態,bool preserveSyncCtx)+ 0x16字節
mscorlib.dll!System.Threading.QueueUserWorkItemCallback。 System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()+ 0x60的字節 mscorlib.dll中!System.Threading.ThreadPoolWorkQueue.Dispatch()+ 0x149字節
mscorlib.dll中!System.Threading._ThreadPoolWaitCallback。PerformWaitCallback()+ 0x5的字節 [原產於託管過渡]

另外在Imediate窗口中鍵入Server.GetLastError().StackTrace.ToString()給出:

在System.Web.StaticFileHandler.GetFileInfo(字符串virtualPathWithPathInfo,字符串physicalPath ,HttpResponse響應) at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context,String overrideVirtualPath) at System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context,AsyncCallback callback,Object state) 在System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在System.Web.HttpApplication.ExecuteStep(IExecutionStep步驟,布爾& completedSynchronously)

+0

什麼是堆棧跟蹤的任何文件?導致異常的代碼在哪裏? – mason

+0

@mason,剛剛添加堆棧跟蹤 – Barka

+0

爲什麼拋出異常?這掩蓋了錯誤發生的地方。看看ex對象並檢查它的屬性。 **但是不要扔掉它!** – mason

回答

1

所以發生了什麼事情是這樣的:我有以下控制方法:

public ActionResult SearchTitle(string path) 
    { 
     //lots of code here 
     return Index(); 
    } 

及以下路線:

 routes.MapRoute("SearchTitle", "{*path}", 
      new { controller = "Home", action = "SearchTitle", path = UrlParameter.Optional }); 

因爲我沒有使用SearchTitle,我快速刪除該方法和相關的路由。這是錯誤的原因。

的解決方案是路由保持控制器中刪除,但改變

 routes.MapRoute("Default", "{*path}", 
      new { controller = "Home", action = "Index", path = UrlParameter.Optional }); 

注:從來就沒有缺少