Getting URL for FileDoesNot Exist with MVC3 and .Net error handling?
Using the following error handling in Application_Error():
protected void Application_Error()
{
Exception exception = Server.GetLastError();
Response.Clear();
HttpException httpException = exception as HttpException;
if (httpException != null)
{
string action;
switch (httpException.GetHttpCode())
{
case 404:
// page not found
开发者_如何学Python action = "HttpError404";
break;
case 500:
// server error
action = "HttpError500";
break;
default:
action = "General";
break;
}
// clear error on server
Server.ClearError();
Response.Redirect(String.Format("~/Error/{0}/?message={1}", action, exception.Message));
}
}
Prior to adding this, my pages rendered fine. Now that I've added this, I'm getting about 21 file not found exceptions. I initially thought that some routing values were wrong (but they don't execute until you try the route), commented them out, same thing.
I stepped through the initial page load up, and since the entire page renders at once, once, I wasn't able to trace down where these are coming from, and the stacktrace doesn't give me what I'm looking for:
at System.Web.StaticFileHandler.GetFileInfo(String virtualPathWithPathInfo, String physicalPath, HttpResponse response)
at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context, String overrideVirtualPath) at System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
I thought that maybe this was occurring where the CSS and scripts are defined, but commenting these out did nothing to fix the issue.
How can I track this down?
I bet 5 bucks it's the favicon.ico
file that some browsers request and which you didn't provide. Inside your error handler simply look at Request.Url
property in Debug mode to see the requested url and you will know which file is missing. Also if you intend to redirect at the end of your error handler as you do, calling Server.ClearError();
doesn't make much sense.
精彩评论