如何解决“文件不存在”异常?

发布于 2024-09-18 20:02:08 字数 714 浏览 14 评论 0原文

我有一个基本的 ASP.NET MVC2 站点,每次加载视图(不是部分视图)时都会记录一个“文件不存在”错误。我很确定这是因为我从母版页引用了一个不存在的文件,但我无法弄清楚它是哪一个。

堆栈跟踪没有用(见下文)。有人对如何最好地调试这个有任何建议吗?

File does not exist. :    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)

I have a basic ASP.NET MVC2 site which logs a single "File does not exist" error every time a view (not partial views) is loaded. I am pretty sure this is because I am referencing a file, from the master page, that does not exist, but I cannot figure out which one it is.

The stack trace is not useful (see below). Does anyone have any tips on how to best debug this?

File does not exist. :    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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

妄断弥空 2024-09-25 20:02:08

正如您所说,它可能是您的母版页引用的丢失文件或图像。要捕获错误,请将以下错误处理程序添加到您的 Global.asax 中,

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

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

然后这应该告诉您页面正在请求什么资源

As you say, its probably a missing file or image referenced by your master page. To capture the error, add the following error handler to your Global.asax

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

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

This should then tell you what resource the page is requesting

回眸一遍 2024-09-25 20:02:08

或者,如果进行调试,您可以将以下行放入“立即窗口”中进行检查:

? HttpContext.Current.Request.Url.ToString()

Or if debugging you can just put the following line in the Immediate Window to check:

? HttpContext.Current.Request.Url.ToString()

那片花海 2024-09-25 20:02:08

检查 CSS 文件中的 url(resource_path),其中“resource_path”不存在。

我也遇到了同样的异常。发现问题出在 CSS 文件中,项目中缺少图像文件,而不是文件系统中。例如,像这样的行:

background-image: url( /images/foo.png );

Where“foo.png”文件不在图像文件夹中。

您可以将最后一个异常转换为 HttpException 以获取 HTML 状态代码:

HttpException httpEx = Server.GetLastError() as HttpException;
if( httpEx != null )
    httpEx.GetHttpCode();         // Will be HTML status code, 404 in this case. 

但它不包含任何文件丢失的信息。我通过检查页面上的每个 CSS 类声明是否发生此错误找到了丢失的文件。

Check your CSS files for url( resource_path ) where "resource_path" doesn't exist.

I was getting the same exception. Found problem was in CSS file where an image file was missing from the project and not on the file system. For example a line like:

background-image: url( /images/foo.png );

Where "foo.png" file isn't in in the images folder.

You can cast the last exception as an HttpException to get the HTML status code:

HttpException httpEx = Server.GetLastError() as HttpException;
if( httpEx != null )
    httpEx.GetHttpCode();         // Will be HTML status code, 404 in this case. 

But it doesn't contain any information what file is missing. I found the missing file by checking every CSS class declaration on the page were this error was occurring.

倒数 2024-09-25 20:02:08

Global.asax 文件 Request.RawUrl 中添加以下方法

protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    Log.Error("An error occurred", ex);
    Log.Error("Requested url: ", Request.RawUrl);
}

给出给出错误的准确 url。

现在在您的日志文件中您应该看到:

Requested url: /favicon.ico

Add following method in Global.asax file

protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    Log.Error("An error occurred", ex);
    Log.Error("Requested url: ", Request.RawUrl);
}

Request.RawUrl Give exact url which give an Error.

Now in your log file you should see:

Requested url: /favicon.ico
此岸叶落 2024-09-25 20:02:08

就我而言,尽管我在浏览器控制台中没有找到任何“404 错误”,但我遇到了相同的错误。我什至检查了主文件,也没有发现任何文件丢失。
事实证明,浏览器期望 favicon.ico 位于网站的根目录。
我在网站的根目录创建了一个 favicon.ico,问题就消失了。
这是一个生成图标的好网站:http://www.favicon.cc/

In my case I was getting the same error, although I did not find any "404 errors" in the browser console. I even checked the Master file and could not find any file missing.
It turned out that the browser expects favicon.ico to be at the root of the site.
I created a favicon.ico at the root of my site and the issue is gone.
This was a good website to generate the icon: http://www.favicon.cc/

狼亦尘 2024-09-25 20:02:08

就我而言,罪魁祸首是缺少默认主页,即default.aspx。

所以添加default.aspx文件解决了问题。

但是,该项目不需要默认页面,因为它不是面向用户的项目。

它更像是一个中间件应用程序,用于通过网络连接遗留系统。

in my case it was a missing default home page i.e default.aspx that was the culprit.

So adding the default.aspx file solved the problem.

However, this project does not require a default page as it's not a user facing project.

It's was more of a middleware app to interface legacy systems over the web.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文