在webforms应用程序中自定义错误页面

时间:2017-02-22 16:30:16

标签: c# asp.net iis webforms web-config

我需要在我的webforms应用程序中自定义一些错误页面。所以我创建了一个新的网络表单404ErrorPage.aspx

public partial class _404ErrorPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // If you're running under IIS 7 in Integrated mode set use this line to override
            // IIS errors:
            Response.TrySkipIisCustomErrors = true;

            // Set status code and message; you could also use the HttpStatusCode enum:
            // System.Net.HttpStatusCode.NotFound
            Response.StatusCode = 404;
            Response.StatusDescription = "Page not found";
        }
    }

404ErrorPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="404ErrorPage.aspx.cs" Inherits="WebApp._404ErrorPage" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page not found</title>
    <style type="text/css">
        body {
            background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAABZ0RVh0Q3JlYXRpb24gVGltZQAxMC8yOS8xMiKqq3kAAAAcdEVYdFNvZnR3YXJlAEFkb2JlIEZpcmV3b3JrcyBDUzVxteM2AAABHklEQVRIib2Vyw6EIAxFW5idr///Qx9sfG3pLEyJ3tAwi5EmBqRo7vHawiEEERHS6x7MTMxMVv6+z3tPMUYSkfTM/R0fEaG2bbMv+Gc4nZzn+dN4HAcREa3r+hi3bcuu68jLskhVIlW073tWaYlQ9+F9IpqmSfq+fwskhdO/AwmUTJXrOuaRQNeRkOd5lq7rXmS5InmERKoER/QMvUAPlZDHcZRhGN4CSeGY+aHMqgcks5RrHv/eeh455x5KrMq2yHQdibDO6ncG/KZWL7M8xDyS1/MIO0NJqdULLS81X6/X6aR0nqBSJcPeZnlZrzN477NKURn2Nus8sjzmEII0TfMiyxUuxphVWjpJkbx0btUnshRihVv70Bv8ItXq6Asoi/ZiCbU6YgAAAABJRU5ErkJggg==);
        }

        .error-template {
            padding: 40px 15px;
            text-align: center;
        }

        .error-actions {
            margin-top: 15px;
            margin-bottom: 15px;
        }

            .error-actions .btn {
                margin-right: 10px;
            }
    </style>
</head>
<body>
    <form id="HtmlForm" runat="server">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="error-template">
                        <h1>Oops!</h1>
                        <h2>404 Not Found</h2>
                        <div class="error-details">
                            Sorry, an error has occured, Requested page not found!

                        </div>
                        <div class="error-actions">
                            <a href="http://www.jquery2dotnet.com" class="btn btn-primary btn-lg"><span class="glyphicon glyphicon-home"></span>
                                Take Me Home </a><a href="http://www.jquery2dotnet.com" class="btn btn-default btn-lg"><span class="glyphicon glyphicon-envelope"></span>Contact Support </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </form>
</body>
</html>

的Global.asax.cs

protected void Application_Error(object sender, EventArgs e)
        {
            // An error has occured on a .Net page.
            var serverError = Server.GetLastError() as HttpException;

            if (null != serverError)
            {
                int errorCode = serverError.GetHttpCode();

                if (404 == errorCode)
                {
                    Server.ClearError();
                    Server.Transfer("404ErrorPage.aspx");
                }
            }
        }

的Web.config

<system.web>


    <customErrors  mode="On" defaultRedirect="404ErrorPage.aspx">
      <error statusCode="403" redirect="404ErrorPage.aspx" />
      <error statusCode="404" redirect="404ErrorPage.aspx" />
    </customErrors>

它不起作用,默认情况下应用程序仍在使用错误页面。

所以我需要知道

  1. 这是什么原因?
  2. 我该如何解决?
  3. 谢谢,

1 个答案:

答案 0 :(得分:0)

感谢@Tieson,我得到了这个答案here

对于IIS&lt; = 6 ,请添加到import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { AppComponent } from './app.component'; import { HttpModule } from '@angular/http'; import { AppRoutingModule} from './app.routing' import { AdminHomeComponent } from './nav/adminhome.component' import { UserHomeComponent } from './nav/userhome.component' import { ContactComponent } from './nav/contact.component' import { LandingComponent } from './nav/mainhome.component' import { LoginFormComponent } from './nav/login.component' import { ShareService } from './nav/ShareService' //import { PaginationModule } from 'ng2-bootstrap'; //import { Ng2PaginationModule } from 'ng2-pagination'; @NgModule({ imports: [BrowserModule, FormsModule, HttpModule, AppRoutingModule ], declarations: [AppComponent, AdminHomeComponent, UserHomeComponent, ContactComponent, LandingComponent, LoginFormComponent], bootstrap: [AppComponent], providers: [ShareService] }) export class AppModule { }

<system.web>

对于IIS7 + ,请添加到<customErrors mode="On" redirectMode="ResponseRewrite"> <error statusCode="404" redirect="/404.aspx"/> <error statusCode="500" redirect="/500.aspx"/> </customErrors>

<system.webServer>