2015-04-23 14 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 }); 

注:从来就没有缺少