ASP.NET Custom Error Page for Web App that uses a Master Page

jp2*_*ode 11 c# asp.net master-pages visual-studio-2010

Reference KB306355: How to create custom error reporting pages in ASP.NET by using Visual C# .NET

I understand how to create a Custom Errors page. There are many examples of how to do it, like in the link above.

None of the examples I have found shows how to do what I am after.

I have a Web Application that uses a Master Page.

In my Master Page, I have a Label control used for errors that all pages will see:

<h4 id="bannerError"><asp:Label ID="lblError" runat="server" /></h4>
Run Code Online (Sandbox Code Playgroud)

In the code behind on that Master Page, I have this:

public void Page_Error(object sender, EventArgs e) {
  var err = Server.GetLastError().GetBaseException();
  ErrorMessage = String.Format("URL {0}: {1} Error: {2}", Request.Url, err.GetType(), err.Message);
  Server.ClearError();
}

public string ErrorMessage {
  get { return lblError.Text; }
  set {
    LogError(value);
    lblError.Text = value;
  }
}
Run Code Online (Sandbox Code Playgroud)

The ErrorMessage is a property. My other pages can easily access it, and I was easily able to edit out the part about writing the error to our server's database.

The Web.config page configuration (snippet):

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <customErrors defaultRedirect="Default.aspx" mode="On">
      <error statusCode="403" redirect="Default.aspx" />
      <error statusCode="404" redirect="Default.aspx" />
    </customErrors>
  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

How would I edit my files so that any errors that occur on any of my pages in my application (that derive from Master Page), simply show this basic information through the Master Page instead of redirecting the page to another URL?

Spr*_*cat 2

您将无法使用控件来设置未处理的页面级错误的错误消息,因为不会创建任何控件(请参阅这篇MS 文章)。您可以捕获页面级别的错误并设置母版页内容,如下所示:

    protected override void OnError(EventArgs e) {
        var err = Server.GetLastError().GetBaseException();
        var errorMessage = String.Format("URL {0}: {1} Error: {2}", Request.Url, err.GetType(), err.Message);
        ((MyMasterPageClass)Master).ShowError(errorMessage);
        Server.ClearError();            
    }
Run Code Online (Sandbox Code Playgroud)

然后在Master Page中直接设置内容:

    public void ShowError(string message) {
        Response.Write(string.Format("<h4 id=\"bannerError\">{0}</h4>", message));
    }
Run Code Online (Sandbox Code Playgroud)

但同样,您的母版页无论如何都不会呈现,因此它有点违背了目的。如果您确实想避免重定向到错误页面,您可以使用 jQuery.get() 之类的 ajax 加载内容,然后根据需要显示结果/错误:

var request = $.get("www.mywebsite.com/Bugs2012.aspx");

request.done(function (data) {
    $("#childContent").html(data);
});

request.fail(function (xhr, status, msg) {
    var displayMsg = "Request could not be completed. ";
    if (status === "error") {
        switch (xhr.status) {
            case 404: 
                displayMsg += "The content could not be found.";
                break;
        }
    }
    $("#bannerError").text(displayMsg);
});
Run Code Online (Sandbox Code Playgroud)

我尝试创建一个jsfiddle,但是由于跨域ajax问题,它有点做作:js fiddle