2017-07-13 172 views
1

我尝试使用IIS为.NET框架应用程序设置自定义错误(使用Visual Studio 2015构建,如果相关)。我们的服务器是Windows Server 2012 R2 。我已经看过其他堆栈溢出解决方案,但没有解决这个问题。我在我的web配置和服务器管理器功能设置中设置了自定义错误。自定义错误页面的路径(是一个静态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> 

我没有在Web配置中看到任何重复的静态内容。我直接在应用程序的服务器管理器中添加了所有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 
     } 
    ); 
    }; 
}; 

然而,这给了我生成错误。

+0

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

回答

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 
    } 
); 

这应该匹配您给定的路线,以正确的控制器。

+1

所以为了澄清,我需要把它放在我的web.config中,默认的重定向应该导向控制器,而不是视图? –

+1

我刚刚修改了web.config,如下所示: <! - 添加任何自定义方法,如果您需要其他错误代码 - > 这是你的想法吗?在运行时,我收到了与以前相同的错误。 –

+1

就是这样。你只需要我的答案中的'customErrors'部分,不要删除'system.web'部分已经有的好东西。然后确保你有一个控制器来提供正确的视图。还要确保应用程序路由配置具有类似于“{controller}/{action}/{id}”'的特性,以便将路由正确映射到控制器。 – tigerswithguitars

相关问题