Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" ....
like image 744
mjb Avatar asked Sep 07 '13 09:09

mjb


2 Answers

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>
like image 154
Der_Meister Avatar answered Nov 14 '22 11:11

Der_Meister


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

like image 20
Manish Sharma Avatar answered Nov 14 '22 13:11

Manish Sharma