如何解决“文件不存在”异常?
我有一个基本的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如您所说,它可能是您的母版页引用的丢失文件或图像。要捕获错误,请将以下错误处理程序添加到您的 Global.asax 中,
然后这应该告诉您页面正在请求什么资源
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
This should then tell you what resource the page is requesting
或者,如果进行调试,您可以将以下行放入“立即窗口”中进行检查:
? 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()
检查 CSS 文件中的 url(resource_path),其中“resource_path”不存在。
我也遇到了同样的异常。发现问题出在 CSS 文件中,项目中缺少图像文件,而不是文件系统中。例如,像这样的行:
Where“foo.png”文件不在图像文件夹中。
您可以将最后一个异常转换为 HttpException 以获取 HTML 状态代码:
但它不包含任何文件丢失的信息。我通过检查页面上的每个 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:
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:
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.
在
Global.asax
文件Request.RawUrl
中添加以下方法给出给出错误的准确 url。
现在在您的日志文件中您应该看到:
Add following method in
Global.asax
fileRequest.RawUrl
Give exact url which give an Error.Now in your log file you should see:
就我而言,尽管我在浏览器控制台中没有找到任何“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/
就我而言,罪魁祸首是缺少默认主页,即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.