【发布时间】: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