当前位置: 首页 > 知识库问答 >
问题:

如何在ASP。NET MVC 4

毛镜
2023-03-14

我想要一个显示500、404和403的自定义错误页面

> < li>

web.config中启用自定义错误,如下所示:

<customErrors mode="On" 
              defaultRedirect="~/Views/Shared/Error.cshtml">

    <error statusCode="403" 
           redirect="~/Views/Shared/UnauthorizedAccess.cshtml" />

    <error statusCode="404" 
           redirect="~/Views/Shared/FileNotFound.cshtml" />

</customErrors>

已将 HandleErrorAttribute 注册为 FilterConfig 类中的全局操作筛选器,如下所示:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new CustomHandleErrorAttribute());
    filters.Add(new AuthorizeAttribute());
}

为上述每条消息创建了一个自定义错误页面。500 的默认版本已经开箱即用。

在每个自定义错误页面视图中声明页面的模型是 System.Web.Mvc.HandleErrorInfo

对于500,它显示自定义错误页面。对其他人来说,情况并非如此。

我错过了什么吗?

当我阅读< code > HandleErrorAttribute 类的< code>OnException方法中的代码时,看起来这并不是显示自定义错误的全部,它只处理500个错误。

我必须执行哪些操作来处理其他错误?

共有3个答案

邢永安
2023-03-14

我做了一些比其他发布的解决方案需要更少编码的事情。

首先,在我的web.config,我有以下几点:

xml prettyprint-override"><customErrors mode="On" defaultRedirect="~/ErrorPage/Oops">
   <error redirect="~/ErrorPage/Oops/404" statusCode="404" />
   <error redirect="~/ErrorPage/Oops/500" statusCode="500" />
</customErrors>

和控制器(/控制器/ErrorPageController.cs)包含以下内容:

public class ErrorPageController : Controller
{
    public ActionResult Oops(int id)
    {
        Response.StatusCode = id;

        return View();
    }
}

最后,该视图包含以下内容(为了简单起见,将其分解,但它可以包含:

@{ ViewBag.Title = "Oops! Error Encountered"; }

<section id="Page">
  <div class="col-xs-12 well">
    <table cellspacing="5" cellpadding="3" style="background-color:#fff;width:100%;" class="table-responsive">
      <tbody>
        <tr>
          <td valign="top" align="left" id="tableProps">
            <img width="25" height="33" src="~/Images/PageError.gif" id="pagerrorImg">
          </td>
          <td width="360" valign="middle" align="left" id="tableProps2">
            <h1 style="COLOR: black; FONT: 13pt/15pt verdana" id="errortype"><span id="errorText">@Response.Status</span></h1>
          </td>
        </tr>
        <tr>
          <td width="400" colspan="2" id="tablePropsWidth"><font style="COLOR: black; FONT: 8pt/11pt verdana">Possible causes:</font>
          </td>
        </tr>
        <tr>
          <td width="400" colspan="2" id="tablePropsWidth2">
            <font style="COLOR: black; FONT: 8pt/11pt verdana" id="LID1">
                            <hr>
                            <ul>
                                <li id="list1">
                                    <span class="infotext">
                                        <strong>Baptist explanation: </strong>There
                                        must be sin in your life. Everyone else opened it fine.<br>
                                    </span>
                                </li>
                                <li>
                                    <span class="infotext">
                                        <strong>Presbyterian explanation: </strong>It's
                                        not God's will for you to open this link.<br>
                                    </span>
                                </li>
                                <li>
                                    <span class="infotext">
                                        <strong> Word of Faith explanation:</strong>
                                        You lack the faith to open this link. Your negative words have prevented
                                        you from realizing this link's fulfillment.<br>
                                    </span>
                                </li>
                                <li>
                                    <span class="infotext">
                                        <strong>Charismatic explanation: </strong>Thou
                                        art loosed! Be commanded to OPEN!<br>
                                    </span>
                                </li>
                                <li>
                                    <span class="infotext">
                                        <strong>Unitarian explanation:</strong> All
                                        links are equal, so if this link doesn't work for you, feel free to
                                        experiment with other links that might bring you joy and fulfillment.<br>
                                    </span>
                                </li>
                                <li>
                                    <span class="infotext">
                                        <strong>Buddhist explanation:</strong> .........................<br>
                                    </span>
                                </li>
                                <li>
                                    <span class="infotext">
                                        <strong>Episcopalian explanation:</strong>
                                        Are you saying you have something against homosexuals?<br>
                                    </span>
                                </li>
                                <li>
                                    <span class="infotext">
                                        <strong>Christian Science explanation: </strong>There
                                        really is no link.<br>
                                    </span>
                                </li>
                                <li>
                                    <span class="infotext">
                                        <strong>Atheist explanation: </strong>The only
                                        reason you think this link exists is because you needed to invent it.<br>
                                    </span>
                                </li>
                                <li>
                                    <span class="infotext">
                                        <strong>Church counselor's explanation:</strong>
                                        And what did you feel when the link would not open?
                                    </span>
                                </li>
                            </ul>
                            <p>
                                <br>
                            </p>
                            <h2 style="font:8pt/11pt verdana; color:black" id="ietext">
                                <img width="16" height="16" align="top" src="~/Images/Search.gif">
                                HTTP @Response.StatusCode - @Response.StatusDescription <br>
                            </h2>
                        </font>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</section>
寿飞飙
2023-03-14

我做过pablo解决方案,但总是有错误(MVC4)

未找到视图“错误”或其主视图,或者没有视图引擎支持搜索的位置。

要解决这个问题,请删除该行

 filters.Add(new HandleErrorAttribute());

在FilterConfig.cs

洪鹏海
2023-03-14

我当前的设置(在MVC3上,但我认为它仍然适用)依赖于ErrorController,所以我使用:

<system.web>
    <customErrors mode="On" defaultRedirect="~/Error">
      <error redirect="~/Error/NotFound" statusCode="404" />
    </customErrors>
</system.web>

并且控制器包含以下内容:

public class ErrorController : Controller
{
    public ViewResult Index()
    {
        return View("Error");
    }
    public ViewResult NotFound()
    {
        Response.StatusCode = 404;  //you may want to set this to 200
        return View("NotFound");
    }
}

视图只是您实现它们的方式。不过,我倾向于添加一些逻辑,以显示应用程序处于调试模式时的堆栈跟踪和错误信息。所以Error.cshtml如下所示:

@model System.Web.Mvc.HandleErrorInfo
@{
    Layout = "_Layout.cshtml";
    ViewBag.Title = "Error";
}
<div class="list-header clearfix">
    <span>Error</span>
</div>
<div class="list-sfs-holder">
    <div class="alert alert-error">
        An unexpected error has occurred. Please contact the system administrator.
    </div>
    @if (Model != null && HttpContext.Current.IsDebuggingEnabled)
    {
        <div>
            <p>
                <b>Exception:</b> @Model.Exception.Message<br />
                <b>Controller:</b> @Model.ControllerName<br />
                <b>Action:</b> @Model.ActionName
            </p>
            <div style="overflow:scroll">
                <pre>
                    @Model.Exception.StackTrace
                </pre>
            </div>
        </div>
    }
</div>
 类似资料:
  • 我最近开始玩ASP。net MVC(4),但我不能对我遇到的这一个问题掉以轻心。我相信当你知道的时候这很容易。 我基本上是在索引视图中尝试执行以下操作: < li >在索引视图中列出“便笺”类型数据库中的当前项目(这很简单) < li >在同一索引视图中创建新项目(不太容易)。 所以我想我需要一个部分视图,并且我已经创建了如下(_CreateNote.cshtml): 在我的原始索引视图(Inde

  • 我有几个传统的ASP. NET Web应用程序,它们共享一个ASP. NET成员资格数据库。我想迁移到使用. NET Core和IdtyServer4的微服务架构,并在新的微服务生态系统中拥有身份服务器来使用现有的ASP. NET成员资格用户存储,但是。NET Core似乎根本不支持ASP. NET成员资格。 我目前有一个概念验证,涉及一个web API、identity server和一个MVC

  • 我试图从JS/Ajax向我的WebAPI发出请求时遇到了问题。在我的解决方案中,我有一个发布在srv02:2400上的Web API,我的网站发布在srv02:2300上 当我导航到页面时http://srv02:2300/all-请求。aspx,页面加载正常,除了应该来自我的API的数据 我得到了一个错误: 但是,如果我把url超文本传输协议://srv02:2400/api/请求/查找/1粘贴

  • 本文向大家介绍asp服务器如何搭建,包括了asp服务器如何搭建的使用技巧和注意事项,需要的朋友参考一下 直接开始搭建asp服务器,大家一起动手操作。 web服务扩展,除了第一二项,其他都允许。 打开Internet信息服务(IIS)管理器 点击默认网站的属性 点击主目录 点击配置——>选项,把启用父路径的勾打上。 点击浏览中,找到以解压的asp网站管理系统路径选上。 点击文档。把启用默认内容文档的

  • 问题内容: 我想使用经典的ASP脚本返回JSON对象(这是AJAX请求的一部分)。 如果我仅以以下形式发送回复: 这将工作,还是我实际上需要JSON库? 编辑: 我正在尝试在http://www.devbridge.com/projects/autocomplete/jquery/#howto上获取自动完成插件。 javascript: ASP: 自动完成功能不起作用。如果我使用像查找这样的本地查

  • 我正在使用Swashbuckle为API生成文档。我的控制器方法如下: Swashbuckle将< code>HttpRequestMessage视为生成的文档中的一个参数。有没有办法配置Swashbuckle忽略< code>HttpRequestMessage,因为它只是出于测试目的才包含在签名中?