在MVC 5自定义错误页 [英] Custom Error pages in MVC 5

查看:203
本文介绍了在MVC 5自定义错误页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在ASP.NET MVC 5,但几乎没有取得圆满成功配置自定义错误页。

I have been trying to configure Custom Error pages in ASP.NET MVC 5 but with little complete success.

要做到这一点,我按照这个指南:的http:// benfoster。 IO /博客/ ASPNET-MVC-自定义错误页

To do this, I have followed this guide: http://benfoster.io/blog/aspnet-mvc-custom-error-pages

Web.config文件的customErrors

Web.config customerrors

<customErrors mode="On" defaultRedirect="~/500.aspx" redirectMode="ResponseRewrite">
  <error statusCode="401" redirect="~/401.aspx" />
  <error statusCode="403" redirect="~/401.aspx" />
  <error statusCode="404" redirect="~/404.aspx" />      
</customErrors>

Web.config文件 httpErrors

<httpErrors errorMode="Custom" >
  <remove statusCode="401" />
  <error statusCode="401" path="401.html" responseMode="File" />
  <remove statusCode="403" />
  <error statusCode="403" path="401.html" responseMode="File" />
  <remove statusCode="404" />
  <error statusCode="404" path="404.html" responseMode="File"/>
  <remove statusCode="500" />
  <error statusCode="500" path="500.html" responseMode="File" />
</httpErrors>

我已删除了 HandleErrorAttribute 从过滤器配置。

通过这个设置,我得到如下结果:

With this setup, I get the following results:


  • 浏览到一个不存在的静态资源结果
    例如: http://my.website.com/fakeresource.htm 结果
    这导致在httpErrors部配置的静态404.htm文件。结果
    URL在浏览器地址栏搜索pserved $ P $
    404状态code设置是否正确。

  • Browse to a non-existent static resource
    Example: http://my.website.com/fakeresource.htm
    This results in the static 404.htm file configured in the httpErrors section.
    The Url is preserved in the browser address bar
    A 404 status code is correctly set.

浏览到一个坏的路线结果
例如: http://my.website/realcontroller/fakeaction 结果
这导致了 HttpException 当匹配的路由无法找到被抛出。结果
将显示404.aspx页面。结果
URL在浏览器地址栏中pserved $ P $。结果
404状态code设置是否正确。

Browse to a bad route
Example: http://my.website/realcontroller/fakeaction
This results in a HttpException being thrown when a matching route cannot be found.
The 404.aspx page is displayed.
The Url is preserved in the browser address bar.
A 404 status code is correctly set.

浏览到一个有效的途径,但与数据库资源无效的ID结果
例如: http://my.website.com/realcontroller/realaction/22

(22是不存在的,行动将返回 HttpNotFound()方法的资源的ID。结果
这导致在httpErrors部配置的静态404.htm文件。结果
URL在浏览器地址栏搜索pserved $ P $
404状态code设置是否正确。结果
如果我扔 HttpException(404,未找到)而不是使用定义的 HttpNotFound(),我得到了404在的customErrors

Browse to a valid route, but with an invalid ID for a database resource
Example: http://my.website.com/realcontroller/realaction/22
(22 is the ID of a resource which does not exist, the Action will return the HttpNotFound() method.
This results in the static 404.htm file configured in the httpErrors section.
The Url is preserved in the browser address bar
A 404 status code is correctly set.
If I throw HttpException(404, "Not Found") instead of using HttpNotFound(), I get the 404 defined in customErrors

异常和放大器;未经授权请求结果
也都正常工作使用的Web.config的该配置。

Exceptions & Unauthorised Requests
Also all work correctly using this configuration of Web.config

然而,检查的ModelState与 ModelSate.IsValid ,或者使用 ModelState.AddModelError(InputName补充说:错误的任何行动,错误消息 ); 导致一个400错误请求

However, any action which checks ModelState with ModelSate.IsValid, or adds errors using ModelState.AddModelError("InputName", "Error Message"); results in a 400 Bad request.

下面是我在IE浏览器和放大器得到错误;铬:

Here are the errors I get in IE & Chrome:

IE 结果

IE

Chrome浏览器结果

Chrome

相反,我应该得到的是原来的观点,与旁边显示哪个验证失败输入的验证消息。

Instead, what I should be getting is the original view, with the validation messages displayed next to the inputs which failed validation.

如果我删除web.config中的 httpErrors 部分,这个问题解决了,但我不再获得定制的IIS错误页面。

If I remove the httpErrors section from web.config, the issue is resolved, but I no longer get custom IIS error pages.

我也试过 existingResponse 属性的每个选项的 httpErrors ,但不同的选项打破无论是错误页面或在的ModelState 验证。

I have also tried every option of the existingResponse property on httpErrors but different options breaks either the error pages or the ModelState validation.

最后,我尝试设置 Response.TrySkipIisCustomErrors = TRUE; 这似乎修复的动作我是测试问题

Finally I tried setting Response.TrySkipIisCustomErrors = true; and this appear to fix the problem in the action I was testing.

但是,这意味着我必须设置上使用的每一个动作的ModelState 此属性,而只是不健全的权利。

But this means that I have to set this property on every action which uses ModelState, and that just doesn't sound right.

我在做什么错了?

更新
为了验证这是否是项目的具体,我创建了VS2012一个新的项目,并配置了相同的错误页面。无奈的是,一切工作。我现在能想到的唯一的区别是,该项目遇到的问题最初开始使用的MVC 4,并升格为MVC 5。

UPDATE To test if this was project specific, I created a new project in VS2012 and configured the same error pages. Frustratingly, everything worked. The only difference I can think of now is that the project experiencing the problem originally started out using MVC 4, and was upgraded to MVC 5.

推荐答案

这样看来,自定义错误,不得不无关,与我遇到的错误的请求。
相反,还有这是正在发布的主要形式不正确时,也被称为页面上的另一种形式。

It would appear that custom errors had nothing to do with the bad request I encountered. Instead, there was another form on the page which was incorrectly also being called when the main form was being posted.

这篇关于在MVC 5自定义错误页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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