Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override HttpErrors section in Web.Config - ASP.NET MVC3

I am trying to add custon 404 pages as per answer of this question

ASP.NET MVC 404 handling and IIS7 <httpErrors>

by adding

<httpErrors existingResponse="PassThrough" />

under <system.webServer> tag in my Web.Config file.

But I am getting following error

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>

I also tried to override the locking based on http://learn.iis.net/page.aspx/145/how-to-use-locking-in-iis-70-configuration ( Task 2 )

by adding location tag in the Web.config

as following

<configuration>
....
....

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

but I am getting the same error.

How should I configure httpErrors element in Web.config so it works

I am using IIS 7 , VS 2010 , ASP.NET MVC3

Update:

I am able to get rid of the locked error

if i modify applicationHost.config file and change

this

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

to

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

but ideally I do not want to change applicationHost.config file and want to override it from the Web.config file

like image 931
N30 Avatar asked May 17 '11 15:05

N30


1 Answers

If you are interested you can create custom errors handling from your 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);

        }

Controller:

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

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

You can have different views for each error. You can also send email from this controller method to you webmaster email account - very handy.

like image 130
bobek Avatar answered Oct 25 '22 12:10

bobek