asp.net-mvc 如何解决异常“文件不存在”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3705057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:29:25  来源:igfitidea点击:

How to solve exception "File does not exist"?

asp.net-mvcapplication-error

提问by ?yvind

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.

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

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)

回答by Clicktricity

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

正如您所说,它可能是您的母版页引用的丢失文件或图像。要捕获错误,请将以下错误处理程序添加到您的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

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

回答by The Coder

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

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

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

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

回答by user764200

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

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

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:

我得到了同样的例外。发现问题出在 CSS 文件中,其中图像文件从项目中丢失,而不是在文件系统中。例如像这样的一行:

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

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

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

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

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

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.

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

回答by Rikin Patel

Add following method in Global.asaxfile

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

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.RawUrlGive exact url which give an Error.

Request.RawUrl给出给出错误的确切网址。

Now in your log file you should see:

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

Requested url: /favicon.ico

回答by Oladipo Olasemo

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

在我的情况下,它是一个丢失的默认主页,即 default.aspx 是罪魁祸首。

So adding the default.aspx file solved the problem.

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

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.

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

回答by Has AlTaiar

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/

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