StrugglingCoder
StrugglingCoder

Reputation: 5011

Error handling in ASP .Net MVC

Being a newbie to MVC, I tried to implement custom error handling in my demo application. I tried a few steps fro this document Error Handling Document but the custom error page for error 404 is not being shown.

Here is what I have tried so far .

My web.config looks like :

enter image description here

And the FilterConfig.cs looks like :

enter image description here

And here is the custom sample error page page404.html for the initial round of testing :

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <h1>Page is not there dude !!</h1>
    Sorry about this.
</body>
</html>

But still when a request a url which is not configured into my application, I still get the trivial page not found error page.

enter image description here

What am I missing ?

Update

I understand next I have to update the error page to be an .aspx page. But for now I just want the initial version to work.

Please find the web.config below :

<?xml version="1.0"?>

<configuration>

  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.web>
    <customErrors mode="On">
      <error statusCode="404" redirect="~/Error/page404.html"/> <!--On all 404 pages, show page404.aspx-->
    </customErrors>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>

    <!--
        Enabling request validation in view pages would cause validation to occur
        after the input has already been processed by the controller. By default
        MVC performs request validation before a controller processes the input.
        To change this behavior apply the ValidateInputAttribute to a
        controller or action.
    -->
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>

    </pages>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />  
  </handlers>
  </system.webServer>
</configuration>

Upvotes: 0

Views: 413

Answers (1)

Midineo
Midineo

Reputation: 141

try to add the block <customErrors> inside <system.web> section ,Like this :

<configuration>
<system.web>
    <customErrors mode="On">
      <error statusCode="404" redirect="~/Error/error.html" />
    </customErrors>
</system.web>
</configuration>

[Update] as per the last screenshot this issue should be appeared if :

  1. customerror mode is off.
  2. statuscode is not matched.

both are not in your web.config , so try iisreset on the server. Also the web.config attached is similar to the one under "view" folder , try web.config under the web application directly

Upvotes: 2

Related Questions