mjb
mjb

Reputation: 7969

How to redirect all httpErrors to custom url?

These are the codes in web.config:

<system.web>
  <customErrors mode="Off" >
  </customErrors>
</system.web>
<system.webServer>
  <httpErrors errorMode="Custom" existingResponse="Replace">
    <clear />
    <error statusCode="404" prefixLanguageFilePath="" path="/ResourceNotFound" responseMode="ExecuteURL" />
    <error statusCode="500" prefixLanguageFilePath="" path="/ResourceNotFound" responseMode="ExecuteURL" />
    </httpErrors>
</system.webServer>

The above settings will redirect httpError of 404 and 500 only.

But instead of manually add all the error code of 400, 401, 403....etc..etc...

Can we just set it redirect all errors to the same url without typing all the error code?

<error statusCode="400" .....
<error statusCode="401" .....
<error statusCode="403" .....
<error statusCode="404" .....
<error statusCode="xxx" ....

Upvotes: 9

Views: 20948

Answers (2)

Der_Meister
Der_Meister

Reputation: 5027

The httpErrors section has defaultPath attribute.

<system.webServer>
  <httpErrors defaultPath="Error.html" defaultResponseMode="File">
    <clear />
  </httpErrors>
</system.webServer>

http://www.iis.net/configreference/system.webserver/httperrors

However, I don't use it, because defaultPath is locked in IIS Express by default. Need to edit %homepath%\Documents\IISExpress\config\applicationHost.config to unlock it.

<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath">
  <!-- ... -->
</httpErrors>

Upvotes: 8

Manish Sharma
Manish Sharma

Reputation: 2426

try this,

add in web.config file.

<system.webServer>
  <httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="File" >
    <remove statusCode="500" />
    <error statusCode="500" prefixLanguageFilePath="C:\Contoso\Content\errors"
    path="500.htm" />
 </httpErrors>
</system.webServer>

and

<httpErrors existingResponse="Replace" defaultResponseMode="ExecuteURL" errorMode="Custom">
    <remove statusCode="404" />
    <error statusCode="404" path="/ErrorPages/Oops.aspx" responseMode="ExecuteURL"/>
    <remove statusCode="401" />
    <error statusCode="401" path="/Account/Login.aspx" responseMode="ExecuteURL"/>
    <remove statusCode="501"/>
    <error statusCode="501" path="/ErrorPages/Oops.aspx" responseMode="ExecuteURL"/>
    <remove statusCode="411"/>
    <error statusCode="411" path="/ErrorPages/Oops.aspx" responseMode="ExecuteURL"/>
    <remove statusCode="403"/>
    <error statusCode="403" path="/ErrorPages/Oops.aspx" responseMode="ExecuteURL"/>
</httpErrors>

and more about this http://www.iis.net/configreference/system.webserver/httperrors

Upvotes: 6

Related Questions