【问题标题】:Runtime Error: An exception occurred while executing the custom error page运行时错误:执行自定义错误页面时发生异常
【发布时间】:2017-07-13 15:35:52
【问题描述】:

我正在尝试使用 IIS 为 .net 框架应用程序设置自定义错误(如果相关,则使用 Visual Studio 2015 构建。)我们的服务器是 windows server 2012 R2。我已经查看了针对此问题的其他堆栈溢出解决方案,但没有一个解决方案。我在我的网络配置和服务器管理器功能设置中设置了自定义错误。自定义错误页面(是静态 html 页面)的路径在服务器管理器中为所有错误状态代码设置。以前的一些堆栈建议禁用 python,所以我尝试了,但它没有解决任何问题。我的网络配置如下:

    <?xml version="1.0"?>
<!--other web config-->
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>
  <system.web>
    <httpRuntime targetFramework="4.5"/>
    <customErrors defaultRedirect="/Views/Home/customError.cshtml" mode="On"/>
    <compilation debug="true"/>
  </system.web>
  <system.webServer>
    <!--<httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="500" subStatusCode="-1" />
      <error statusCode="500" subStatusCode="-1" responseMode="ExecuteURL" path="/Views/Home/customError.cshtml" />
    </httpErrors>-->
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <connectionStrings>
    <add name="Entities" connectionString="***" providerName="System.Data.EntityClient"/>
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0"/>
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

我没有在网络配置中看到任何重复的静态内容。我直接在应用程序的服务器管理器中添加了所有 iis 错误页面重定向。

当我在本地运行应用程序并尝试触发 404 错误时,我收到运行时错误:处理您的请求时发生异常。此外,在执行第一个异常的自定义错误页面时发生了另一个异常。请求已终止。 当我通过服务器运行它(也试图触发 404 错误)时,它会加载到上一页。

我不确定如何解决这个问题。任何见解都会很棒!谢谢!

控制器相关部分:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Net.Mail;
using BE.Models;
using BE.ViewModels;

namespace BE.Controllers
{

    public class HomeController : Controller
    {

        private Entities db = new Entities();

        // Uri Route: "~/Home"
        // View Route: "/Views/Home/Index.cshtml"
        public ActionResult Index()
        {
            return View();
        }


        // Route: "~/Home/customError"
        // View Route: "/Views/Home/customError.cshtml"
        public ActionResult customError()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }


        public ActionResult Contact()
        {
            return View();
        }



        public ActionResult Resources()
        {
            return View();
        }

    }
}

我还将 global.asax.cs 修改为如下所示:

namespace BE
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
        RouteTable.Routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
         new { 
             controller = "Home", 
             action = "Index", 
             id = UrlParameter.Optional
        }
    );
    };
};

但是,这给了我构建错误。

【问题讨论】:

  • 您指向的是 .cshtml 文件,而不是正确的 HTML 文件。也许这就是问题所在?

标签: c# asp.net iis custom-error-pages custom-errors


【解决方案1】:

在 MVC 中,可以重定向到将为页面提供服务的路由。这通常由控制器表示,而不是视图。除非视图是静态 html 页面。当您使用.cshtml 时,我假设您正在从 MVC 控制器提供内容。您的部分应如下所示:

<system.web>
    <!-- Default redirect to the ErrorController index -->
    <customErrors mode="On" defaultRedirect="~/Error">
        <!-- Add any custom methods if you want for other error codes -->
        <error redirect="~/Error/NotFound" statusCode="404" />
    </customErrors>
</system.web>

还要注意使用~/ 表示路径是相对于应用根目录的。

所以请确保在您的控制器文件夹中有一个类似的类:

public class ErrorController : Controller {
    // Uri Route: "~/Error"
    // View Route: "/Views/Error/Index.cshtml"
    public ActionResult Index() {
        return View();
    }

    // Route: "~/Error/NotFound"
    // View Route: "/Views/Error/NotFound.cshtml"
    public ActionResult NotFound() {
        return View();
    }
}

Global.asx.cs 或在App_Start 文件夹中注册的路由中,应该有这样的路由映射:

RouteTable.Routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
     new { 
         controller = "Home", 
         action = "Index", 
         id = UrlParameter.Optional 
     }
);

这应该与您给定的路由匹配到正确的控制器。

【讨论】:

  • 所以澄清一下,我需要把它放在我的 web.config 中,默认重定向应该指向控制器,而不是视图?
  • 我刚刚将 web.config 修改为如下所示: 你是这么想的吗?在运行它时,我收到了与以前相同的错误。
  • 就是这样。您只需要我的答案中的customErrors 部分,不要删除system.web 部分中已有的好东西。然后确保您有一个控制器来提供正确的视图。还要确保应用路由配置有类似"{controller}/{action}/{id}" 这样的路由正确映射到控制器。
  • 我也试过这个,它也收到了同样的错误
  • 名为HomeController的控制器映射到控制器路由/Home,应用程序将删除控制器名称。路线的下一部分是方法。所以,Home/Get 将映射到HomeControllerGet() 方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2016-05-02
  • 2017-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多