ASP.NET MVC 5中的自定义错误页面 [英] Custom error pages in ASP.NET MVC 5

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

问题描述

我想将自定义错误页面添加到我的项目中. 我发现关于我的问题的帖子,我尝试实现它.

I want to add custom error pages to my project. I found this post about my problem and i try to implement it.

所以:

  • 我添加404.cshtml404.html500.cshtml500.html页面
  • 在添加的cshtml文件中设置响应状态代码
  • 评论将HandleErrorAttribute添加到全局过滤器
  • 更新我的web.config文件
  • i add 404.cshtml, 404.html, 500.cshtml and 500.html pages
  • set response status code in added cshtml files
  • comment adding HandleErrorAttribute to global filters
  • update my web.config file

但是现在当我尝试通过路径http://localhost:120/foo/bar我的应用程序位于http://localhost:120的位置时,我进入了下一页:

But now when i try to go by path http://localhost:120/foo/bar where my app is on http://localhost:120 i get next page :

"/"应用程序中的服务器错误.

Server Error in '/' Application.

运行时错误

描述:处理您的请求时发生异常.此外,在执行第一个异常的自定义错误页面时发生了另一个异常.该请求已被终止.

Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.

我设置了<customErrors mode="Off"以查看是什么问题.这是-The resource cannot be found.这是合乎逻辑的.但是当我设置<customErrors mode="On"时-我再次遇到运行时错误.

I set <customErrors mode="Off" to see what problem is. It was - The resource cannot be found. which is logical. But when i set <customErrors mode="On" - i again get Runtime error.

是什么原因引起的,如何解决?

What can cause it and how to solve it?

我的配置文件:

<system.web>
    <customErrors mode="Off" redirectMode="ResponseRewrite" defaultRedirect="~/500.cshtml">
        <error statusCode="404" redirect="~/404.cshtml"/>
        <error statusCode="500" redirect="~/500.cshtml"/>
    </customErrors>
</system.web>

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


IIS 8.5版


IIS version 8.5

推荐答案

您使用的是哪个版本的IIS?如果为7+,则使用<customErrors mode="Off">忽略自定义错误,然后使用<httpErrors>.使用下面的方法将在不更改URL的情况下显示错误页面,而IMO是处理这些问题的首选方法.

What version of IIS are you using? If 7+ then ignore custom errors using <customErrors mode="Off"> and use <httpErrors>. Using the method below the latter will show your error page without changing the URL which IMO is the preferred way of handling these things.

设置一个错误控制器,然后将404和500放在其中,如下所示:

Set up an Error Controller and put your 404 and 500 in there like so:

<httpErrors errorMode="Custom" existingResponse="Replace">
    <remove statusCode="404" />
    <remove statusCode="500" />
    <error statusCode="404" path="/error/notfound" responseMode="ExecuteURL" />
    <error statusCode="500" path="/error/servererror" responseMode="ExecuteURL" />
 </httpErrors>

在控制器中:

public class ErrorController : Controller
{
    public ActionResult servererror()
    {
        Response.TrySkipIisCustomErrors = true;
        Response.StatusCode = (int)HttpStatusCode.InternalServerError;
        return View();
    }

    public ActionResult notfound()
    {
        Response.TrySkipIisCustomErrors = true;
        Response.StatusCode = (int)HttpStatusCode.NotFound;
        return View();
    }

}

然后显然可以为所涵盖的每个错误设置相应的视图.

Then obviously set up the corresponding view for each error covered.

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

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