其他错误找我Error.cshtml,为什么不404错误找Error.cshtml? [英] Other errors find my Error.cshtml, why doesn't 404 error find Error.cshtml?

查看:250
本文介绍了其他错误找我Error.cshtml,为什么不404错误找Error.cshtml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理ASP.NET,MVC 3,网络开发,第一次,都在同一时间。请原谅我,因为我知道这个问题已经从不同的角度讨论了巨资。我还没有找到答案我的具体问题:为什么没有我的应用程序找到我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?

(环境:Win 7的64位,IIS7,SQL 2008前preSS,VS2010,ASP.NET 4,MVC3,EF V4)

(Environment: Win 7 64bit, IIS7, SQL 2008 Express, VS2010, ASP.NET 4, MVC3, EF v4)

我有一个控制器,WorkerController.cs,能够正确读取并写入数据库。如果我更改数据库名称没有更新我的DbContext,它给了我一个错误。当我改变web.config中始终显示自定义错误,让我看到/Views/Shared/Error.cshtml文件。

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.

我的的有FooController.cs文件。如果我去/美孚,我得到一个404错误,符合市场预期。它告诉我它无法找到资源/美孚。

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.

当我设置的customErrors模式=开,使一个HTTP请求到/美孚,我得到一个404错误说/Error.cshtml无法找到。

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.

我寻找和阅读的讨论处理错误与指定控制器的各种方法的帖子,但我真的想知道我错过了什么。为什么会找到/Error.cshtml其他错误,但不是404错误?

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?

推荐答案

除了设置的customErrors =ON,你定义404错误特定的重定向?

Other than setting customErrors="On", have you defined a specific redirect for 404 errors?

如果你有,比如说,一个 ErrorController 设置你的web.config,比如,如:

If you have, say, an ErrorController setup your web.config, for instance, like:

<customErrors mode="On" defaultRedirect="/error/Problem">
    <error statusCode="404" redirect="error/FileNotFound"/>
</customErrors>

或你preFER静态HTML页面为你的错误:

Or of you'd prefer static html pages for your errors:

<customErrors mode="On" defaultRedirect="Problem.html">
  <error statusCode="404" redirect="FileNotFound.html"/>
</customErrors>

您可能想看看一些更多的信息,这样其他的问题:
ASP.NET MVC的HandleError

You might want to take a look at this other question for some more information: ASP.NET MVC HandleError.

有关改进的错误在MVC处理,不过,你也可以看看ELMAH(错误日志记录模块和处理程序):

For improved error handling in MVC, though, you could also take a look at ELMAH (Error Logging Modules and Handlers):


  • 您可以(应该)!得到ELMAH作为的NuGet包;安装程序会做的大部分设置为您

  • 看看<一个href=\"http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx\">Scott Hanselman的入门后

  • 检查这个问题/关于如何使用ASP.NET MVC使用ansers:<一href=\"http://stackoverflow.com/questions/766610/how-to-get-elmah-to-work-with-asp-net-mvc-handleerror-attribute\">How获得ELMAH与ASP.NET MVC [的HandleError]属性来上班?

  • You can (should)! get ELMAH as a NuGet package; the installer will do most of the setup for you
  • Take a look at Scott Hanselman's "introductory" post
  • Check this question/ansers on how to use it with ASP.NET MVC: How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?

这篇关于其他错误找我Error.cshtml,为什么不404错误找Error.cshtml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆