其他错误找到我的Error.cshtml,为什么404错误找不到Error.cshtml?
我第一次同时处理 ASP.NET、MVC 3、Web 开发。请耐心听我说,因为我知道这个主题已经从不同角度进行了大量讨论。我仍然没有找到我的具体问题的答案:为什么我的应用程序在发生 404 时找不到我的 Error.cshtml 文件,而它发现它与其他错误一起正常?
(环境:Win 7 64位、IIS7、SQL 2008 Express、VS2010、ASP.NET 4、MVC3、EF v4)
我有一个控制器 WorkerController.cs,它可以正确地从数据库读取和写入。如果我更改数据库名称而不更新 DbContext,则会出现错误。当我更改 web.config 以始终显示自定义错误时,显示 /Views/Shared/Error.cshtml 文件。
我没有有 FooController.cs 文件。如果我转到 /Foo,我会收到 404 错误,正如预期的那样。它告诉我它找不到资源/Foo。
当我设置 customErrors mode="On" 并向 /Foo 发出 http 请求时,收到 404 错误,指出找不到 /Error.cshtml。
我正在搜索并阅读讨论指定控制器处理错误的各种方法的帖子,但我真的想知道我错过了什么。为什么其他错误会找到 /Error.cshtml,但 404 错误却找不到?
I am tackling ASP.NET, MVC 3, web development, for the first time, all at the same time. Please bear with me, as I know this subject has been discussed heavily from different angles. I still have not found the answer to my specific question: Why doesn't my application find my Error.cshtml file when a 404 occurs, when it finds it just fine with other errors?
(Environment: Win 7 64bit, IIS7, SQL 2008 Express, VS2010, ASP.NET 4, MVC3, EF v4)
I have a controller, WorkerController.cs, that correctly reads and writes from the database. If I change the database name without updating my DbContext, it gives me an error. When I change web.config to always show custom errors, shows me the /Views/Shared/Error.cshtml file.
I do not have a FooController.cs file. If I go to /Foo, I get a 404 error, as expected. It tells me it cannot find the resource /Foo.
When I set customErrors mode="On" and make an http request to /Foo, I get a 404 error saying that /Error.cshtml cannot be found.
I am searching for and reading the posts that discuss the various methods of handling errors with designated controllers, but I really want to know what I'm missing. Why does it find /Error.cshtml for other errors, but not the 404 error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了设置
customErrors="On"
之外,您是否为 404 错误定义了特定的重定向?例如,如果您有一个
ErrorController
设置您的 web.config,例如:或者您更喜欢静态 html 页面来显示错误:
您可能想看看另一个问题欲了解更多信息:
ASP.NET MVC HandleError。
不过,为了改进 MVC 中的错误处理,您还可以看看 ELMAH(错误记录模块和处理程序):
Other than setting
customErrors="On"
, have you defined a specific redirect for 404 errors?If you have, say, an
ErrorController
setup your web.config, for instance, like:Or of you'd prefer static html pages for your errors:
You might want to take a look at this other question for some more information:
ASP.NET MVC HandleError.
For improved error handling in MVC, though, you could also take a look at ELMAH (Error Logging Modules and Handlers):