I am trying to understand what error occurred that sent me to the Global ASAX OnError handler.
我试图了解发送给我的全局ASAX OnError处理程序发生了什么错误。
using System;
using System.Web;
namespace GLSS.Components.HttpModules
{
public class ExceptionModule : System.Web.IHttpModule
{
private void OnError(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
//get the last error
Exception ex = context.Server.GetLastError();
if(ex.InnerException.GetType().ToString() == "CSLA.DataPortalException")
ex = ex.InnerException;
Here is my Exception converted to a String
这是我的Exception转换为String
HttpContext.Current.Server.GetLastError().Message
"File does not exist."
HttpContext.Current.Server.GetLastError().StackTrace
" at System.Web.StaticFileHandler.GetFileInfo(String virtualPathWithPathInfo, String physicalPath, HttpResponse response)
at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context)
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)"
How do I determine what line of code is causing this error? I tried to set my Debug options to break when the error occurs, but it didn't , I still end up in the ONERROR global handler.
如何确定导致此错误的代码行?我试图将我的Debug选项设置为在发生错误时中断,但事实并非如此,我仍然在ONERROR全局处理程序中。
One thing is that I see that the code assumes that there will be an Inner Exception, and this appears to be NULL and is causing a second error within the handler.
有一件事是我看到代码假定会有一个内部异常,这似乎是NULL并导致处理程序中的第二个错误。
I assume that the error is occuring somewhere in compiled code. I checked the Web.Config, and the only paths mentioned there is a Log Path, and that seems valid and logging appears to be working.
我假设错误发生在编译代码的某处。我检查了Web.Config,其中提到的唯一路径是一个日志路径,这似乎有效,并且日志记录似乎正在工作。
Update I found some additional information here:
更新我在这里找到了一些其他信息:
How to solve exception "File does not exist"?
如何解决异常“文件不存在”?
When I check this in the Immediate window:
当我在立即窗口中检查时:
? HttpContext.Current.Request.Url.ToString()
"http://localhost:2322/favicon.ico"
However, what puzzles me is that I search my entire solution looking for favicon.ico using "Find In Files" and I see no reference.
然而,让我感到困惑的是,我使用“在文件中查找”搜索我的整个解决方案,寻找favicon.ico,我看不到任何参考。
Why am I getting an error that the icon file is not found when I see no reference to it? I'm guess some assembly is using it? But why is it looking for it in the web root?
为什么我看到没有找到图标文件时会出现错误?我猜有些装配正在使用它?但是为什么它在Web根目录中寻找呢?
1 个解决方案
#1
15
The request for favicon.ico is being blindly made by most modern browsers and they are expecting a 404 (File Not Found) if there is no favicon (this is proper behaviour). Below you can find a quote from HTML5 working draft regarding Link type "icon" :
大多数现代浏览器都盲目地提出了对favicon.ico的请求,如果没有favicon(这是正确的行为),他们期望404(找不到文件)。您可以在下面找到关于链接类型“icon”的HTML5工作草案的引用:
In the absence of a link with the icon keyword, for documents obtained over HTTP or HTTPS, user agents may instead attempt to fetch and use an icon with the absolute URL obtained by resolving the URL /favicon.ico against the document’s address, as if the page had declared that icon using the icon keyword.
如果没有带有icon关键字的链接,对于通过HTTP或HTTPS获取的文档,用户代理可能会尝试获取并使用带有绝对URL的图标,该图标是通过将URL /favicon.ico解析为文档的地址而获得的,就好像页面使用icon关键字声明了该图标。
The reason why you see the exception is that the web development server or IIS configured to use Managed/Integrated Pipeline Mode puts all requests through Global.asax (including errors).
您看到异常的原因是配置为使用托管/集成管道模式的Web开发服务器或IIS通过Global.asax放置所有请求(包括错误)。
You can try to prevent the browsers from making the requests by creating following dummy link to favicon:
您可以尝试通过创建以下关于favicon的虚拟链接来阻止浏览器发出请求:
<html>
<head>
<link rel="shortcut icon" href="#" />
...
</head>
...
</html>
You also might try one of following:
您也可以尝试以下方法之一:
-
Add following line at beginning of
RegisterRoutes
method:在RegisterRoutes方法的开头添加以下行:
routes.IgnoreRoute("favicon.ico");
or even more extended version:
甚至更多的扩展版本:
routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"});
routes.IgnoreRoute(“{* favicon}”,new {favicon = @“(。* /)?favicon.ico(/.*)?”});
- Create yourself an empty file for favicon
- Filter out the errors by checking
HttpException.GetHttpCode()
for 404 and((System.Web.HttpApplication)Sender).Context.Request.Url
for /favicon.ico.
为自己创建一个空文件
通过检查404的HttpException.GetHttpCode()和/((System.Web.HttpApplication)Sender).Context.Request.Url for /favicon.ico来筛选出错误。
#1
15
The request for favicon.ico is being blindly made by most modern browsers and they are expecting a 404 (File Not Found) if there is no favicon (this is proper behaviour). Below you can find a quote from HTML5 working draft regarding Link type "icon" :
大多数现代浏览器都盲目地提出了对favicon.ico的请求,如果没有favicon(这是正确的行为),他们期望404(找不到文件)。您可以在下面找到关于链接类型“icon”的HTML5工作草案的引用:
In the absence of a link with the icon keyword, for documents obtained over HTTP or HTTPS, user agents may instead attempt to fetch and use an icon with the absolute URL obtained by resolving the URL /favicon.ico against the document’s address, as if the page had declared that icon using the icon keyword.
如果没有带有icon关键字的链接,对于通过HTTP或HTTPS获取的文档,用户代理可能会尝试获取并使用带有绝对URL的图标,该图标是通过将URL /favicon.ico解析为文档的地址而获得的,就好像页面使用icon关键字声明了该图标。
The reason why you see the exception is that the web development server or IIS configured to use Managed/Integrated Pipeline Mode puts all requests through Global.asax (including errors).
您看到异常的原因是配置为使用托管/集成管道模式的Web开发服务器或IIS通过Global.asax放置所有请求(包括错误)。
You can try to prevent the browsers from making the requests by creating following dummy link to favicon:
您可以尝试通过创建以下关于favicon的虚拟链接来阻止浏览器发出请求:
<html>
<head>
<link rel="shortcut icon" href="#" />
...
</head>
...
</html>
You also might try one of following:
您也可以尝试以下方法之一:
-
Add following line at beginning of
RegisterRoutes
method:在RegisterRoutes方法的开头添加以下行:
routes.IgnoreRoute("favicon.ico");
or even more extended version:
甚至更多的扩展版本:
routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"});
routes.IgnoreRoute(“{* favicon}”,new {favicon = @“(。* /)?favicon.ico(/.*)?”});
- Create yourself an empty file for favicon
- Filter out the errors by checking
HttpException.GetHttpCode()
for 404 and((System.Web.HttpApplication)Sender).Context.Request.Url
for /favicon.ico.
为自己创建一个空文件
通过检查404的HttpException.GetHttpCode()和/((System.Web.HttpApplication)Sender).Context.Request.Url for /favicon.ico来筛选出错误。