Enable custom bugs in Azure

I cannot configure IIS to serve my own error pages for errors outside the MVC pipeline. If I throw an exception inside the controller, then the entire file, the Application_Error event, processes the following:

    protected void Application_Error(object sender, EventArgs e)
    {
        var error = Server.GetLastError();

        var routeData = new RouteData();
        routeData.Values["Controller"] = "Error";
        routeData.Values["Area"] = "";
        routeData.Values["Exception"] = error;

        if (error is HttpException)
        {
            switch (((HttpException)error).GetHttpCode())
            {
                case 401:
                    routeData.Values["Action"] = "NotAllowed";
                    break;

                case 403:
                    routeData.Values["Action"] = "NotAllowed";
                    break;

                case 404:
                    routeData.Values["Action"] = "NotFound";
                    break;
                default:
                    routeData.Values["Action"] = "ServerError";
                    break;
            }
        }
        else
        {
            routeData.Values["Action"] = "ServerError";
        }

        Response.Clear();
        Server.ClearError();

        IController controller = new ErrorController();
        controller.Execute(new RequestContext(new HttpContextWrapper(((MvcApplication)sender).Context), routeData));
    }

However, if I go to a non-existent URL, IIS will handle 404 error (or any other error), giving me a standard IIS error message and completely ignoring my web.config settings:

 <system.web>
  <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/error">
    <error statusCode="401" redirect="/error/notallowed" />
    <error statusCode="403" redirect="/error/notallowed" />
    <error statusCode="404" redirect="/error/notfound" />
    <error statusCode="500" redirect="/error/servererror" />
  </customErrors>
</system.web>

<system.webServer>
  <httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
    <clear />
    <error statusCode="401" path="/error/notallowed" />
    <error statusCode="403" path="/error/notallowed" />
    <error statusCode="404" path="/error/notfound" />
    <error statusCode="500" path="/error/servererror" />
  </httpErrors>
</system.webServer>

IIS default error page

/ error / * is handled by the controller inside my application. What can I do to make IIS follow its own error path instead of giving me a standard error page?

This is ASP.NET MVC 3 running on Azure. It also does not work under direct IIS, but the development server runs the controller.

+5
4

IIS defaultResponseMode. responseMode , :

<httpErrors errorMode="Custom">
  <clear />
  <error statusCode="401" path="/error/notallowed" responseMode="ExecuteURL" />
  <error statusCode="403" path="/error/notallowed" responseMode="ExecuteURL" />
  <error statusCode="404" path="/error/notfound" responseMode="ExecuteURL" />
  <error statusCode="500" path="/error/servererror" responseMode="ExecuteURL" />
</httpErrors>

, .

+9

, (, "picture.jpg" ).

Azure WebSites, Azure WebRole. web.config, .

ASPX , HTTP (IIS HTTP 200, .htm)

~/404.aspx:

<%@ Page Language="C#" AutoEventWireup="false" ContentType="text/html" Debug="false" EnableEventValidation="false" EnableSessionState="False" EnableViewState="false" ResponseEncoding="utf-8" Transaction="Disabled" ValidateRequest="false" ViewStateMode="Disabled" %>
<% Context.Response.StatusCode = 404; %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>404 Error - Page not found</title>
</head>
<body>
  <h1>Custom 404 page</h1>
</body>
</html>

~/500.aspx:

<%@ Page Language="C#" AutoEventWireup="false" ContentType="text/html" Debug="false" EnableEventValidation="false" EnableSessionState="False" EnableViewState="false" ResponseEncoding="utf-8" Transaction="Disabled" ValidateRequest="false" ViewStateMode="Disabled" %>
<% Context.Response.StatusCode = 500; %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>500 Server Error</title>
</head>
<body>
  <h1>Custom 500 page</h1>
</body>
</html>

~/Web.config :

<?xml version="1.0"?>
<configuration>
  <system.web>
    <customErrors mode="Off" />
  </system.web>

  <system.webServer>
    <httpErrors errorMode="Detailed" />
  </system.webServer>
</configuration>

~/Web.Release.config :

<?xml version="1.0"?>
<configuration>
  <customErrors mode="On" defaultRedirect="/500.aspx" redirectMode="ResponseRewrite" xdt:Transform="Replace">
    <!-- AWARE do NOT use address in form "~/404.htm" but in "/404.htm" -->
    <error statusCode="404" redirect="/404.aspx" />
    <error statusCode="500" redirect="/500.aspx"/>
  </customErrors>

  <system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace" xdt:Transform="Replace">
      <!-- AWARE do NOT use address in form "~/404.htm" but in "/404.htm" -->
      <remove statusCode="404" />
      <error statusCode="404" path="/404.aspx" responseMode="ExecuteURL" />

      <remove statusCode="500" />
      <error statusCode="500" path="/500.aspx" responseMode="ExecuteURL" />
    </httpErrors>
  </system.webServer>
</configuration>

.

+3

I had exactly this problem, and I was on Azure, the following solution for me:

    Response.TrySkipIisCustomErrors = true;
+3
source

Make sure you set this setting.

<system.webServer>

    <modules runAllManagedModulesForAllRequests="true"/>

</system.webServer>

Otherwise, your IIS application can also run in classic mode instead of integrated mode, which is the IIS setting, however, I thought that the built-in mode was the default in Azure (but to be honest, I did not check)

0
source

All Articles