使用 MVC3 和 .Net 错误处理获取 FileDoesNot Exist 的 URL?
在 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
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));
}
}
在添加此之前,我的页面呈现良好。现在我已经添加了这个,我收到了大约 21 个文件未找到的异常。我最初认为某些路由值是错误的(但在您尝试路由之前它们不会执行),将它们注释掉,同样的事情。
我逐步完成了初始页面加载,由于整个页面一次渲染,我无法追踪这些页面的来源,并且堆栈跟踪没有给我我正在寻找的内容:
在 System.Web.StaticFileHandler.GetFileInfo(String virtualPathWithPathInfo、字符串physicalPath、HttpResponse 响应)
在 System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext 上下文,字符串 overrideVirtualPath)位于 System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext 上下文, AsyncCallback 回调,对象状态)位于 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤, 布尔&同步完成)
我认为这可能是在定义 CSS 和脚本的地方发生的,但是注释掉这些并不能解决问题。
我怎样才能追踪到这个?
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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我打赌 5 美元,这是某些浏览器请求的
favicon.ico
文件,而您没有提供该文件。在错误处理程序中,只需在调试模式下查看Request.Url
属性即可查看请求的 url,您就会知道缺少哪个文件。此外,如果您打算像您一样在错误处理程序末尾进行重定向,则调用 Server.ClearError(); 没有多大意义。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 atRequest.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, callingServer.ClearError();
doesn't make much sense.