如何在Web.Config中覆盖HttpErrors部分- ASP.NET MVC3

10

我试图根据这个问题的回答添加自定义404页面。

ASP.NET MVC 404处理和IIS7 <httpErrors>

通过添加:

<httpErrors existingResponse="PassThrough" />
在我的Web.Config文件中的<system.webServer>标签下,但我收到了以下错误。
Module: CustomErrorModule 
Notification: SendResponse 
Handler: System.Web.Mvc.MvcHandler 
Error Code: 0x80070021 
Config Error: This configuration section cannot be used at this path. 
              This happens when the section is locked at a parent level. 
              Locking is either by default (overrideModeDefault="Deny"), 
              or set explicitly by a location tag with overrideMode="Deny" 
              or the legacy allowOverride="false".  


 Config Source
  153:       <httpErrors existingResponse="PassThrough" />
  154:   </system.webServer>

我也尝试根据http://learn.iis.net/page.aspx/145/how-to-use-locking-in-iis-70-configuration(任务2)覆盖锁定方式,方法是在Web.config中添加位置标签,如下所示:

<configuration>
....
....

  <location  allowOverride="true">
        <system.webServer>
            <httpErrors existingResponse="PassThrough" />
        </system.webServer>
  </location>
</configuration>

但是我仍然得到相同的错误。

我应该如何配置Web.config中的httpErrors元素,以使其正常工作?

我正在使用IIS 7,VS 2010,ASP.NET MVC3。

更新:

我已经成功摆脱了锁定错误

如果我修改applicationHost.config文件并更改

这个

<section name="httpErrors" overrideModeDefault="Deny" />
to
<section name="httpErrors" overrideModeDefault="Allow" />

但是理想情况下,我不希望更改applicationHost.config文件,而是想要通过Web.config文件来覆盖它


你有没有尝试过使用existingResponse="Replace"而不是"PassThrough"? - WiredPrairie
仅供Google使用; 德语错误消息为Dieser Konfigurationsabschnitt kann in diesem Pfad nicht verwendet werden. Dies ist der Fall, wenn der Abschnitt auf übergeordneter Ebene gesperrt ist. Die Sperrung erfolgt standardmäßig (overrideModeDefault="Deny") oder wird explizit mit einem location-Tag mit overrideMode="Deny" oder der Legacyeinstellung allowOverride="false" festgelegt. - Uwe Keim
1
可以在 httpErrors 中使用 configSource 吗? - Kiquenet
1个回答

3
如果您有兴趣,可以在Global.asax.cs中创建自定义错误处理:
protected void Application_Error()
        {

                var exc = Server.GetLastError();
                var httpExc = exc as HttpException;
                Response.Clear();
                Server.ClearError();
                var routeData = new RouteData();
                routeData.Values["controller"] = "Error";
                routeData.Values["action"] = "General";
                routeData.Values["exception"] = exc;
                Response.StatusCode = 500;
                if (httpExc != null)
                {
                    Response.StatusCode = httpExc.GetHttpCode();
                    routeData.Values["action"] = "Http404";
                    routeData.Values["exception"] = httpExc;
                }
                Response.TrySkipIisCustomErrors = true;
                IController errorsController = new WWS_Website.Controllers.ErrorController();
                var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
                errorsController.Execute(rc);

        }

控制器:

public ActionResult General(Exception exception)
{
  return View(exception);
}

public ActionResult Http404(Exception exception)
{
  return View(exception);
}

您可以为每个错误设置不同的显示方式。您还可以从此控制器方法发送电子邮件到您的网络管理员电子邮件帐户-非常方便。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接