Can I ask about why the output in GeneralError.aspx.cs cannot display on GeneralError.aspx page, please?
Because I have tried to change a lot of methods in some my codes of application error based on search google (but not suitable) but cannot display the response.write (that I code in GeneralError.aspx.cs) on GeneralError.aspx but display nothing in GeneralError.aspx. Can you help me to check and explain my errors in it and its solutions, please?
What I have tried:
I paste some these problems code here as below:
For ApplicationError.aspx,
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ApplicationError.aspx.cs" Inherits="WebApplication1.ApplicationError" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Error Handling</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center">
<p style="width:60%; padding:30px 20px 30px 20px; border:solid 3px black">
<asp:TextBox ID="TextBox1" runat="server" Height="43px" Width="66px"
Font-Bold="True" Font-Names="Comic Sans MS" Font-Size="X-Large"></asp:TextBox>
<img alt="" src="images/Divide.jpg" style="height: 39px; width: 65px" />
<asp:TextBox ID="TextBox2" runat="server" Height="43px" Width="66px"
Font-Bold="True" Font-Names="Comic Sans MS" Font-Size="X-Large"></asp:TextBox>
<asp:ImageButton ID="EqualButton" runat="server" Height="44px"
ImageUrl="~/images/EqualButton.jpg" Width="50px"
onclick="EqualButton_Click" />
<asp:Label ID="Label1" runat="server" Font-Bold="True"
Font-Names="Comic Sans MS" Font-Size="XX-Large" ForeColor="Blue" Text="?"></asp:Label>
<br />
<asp:Label ID="lblMessage" runat="server" Text="" Font-Names="Arial" ForeColor="Red"></asp:Label>
</p>
</div>
<p>
</p>
</form>
</body></html>
For ApplicationError.aspx.cs,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class ApplicationError : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void EqualButton_Click(object sender, ImageClickEventArgs e)
{
lblMessage.Text = "";
Label1.Text = "?";
double dblAns = 0.0;
double dblNo1 = Convert.ToDouble(TextBox1.Text);
double dblNo2 = Convert.ToDouble(TextBox2.Text);
dblAns = Divide(dblNo1, dblNo2);
Label1.Text = dblAns.ToString();
}
protected double Divide(double no1, double no2)
{
if (no2 == 0)
{
DivideByZeroException err = new DivideByZeroException();
throw err;
}
else
return no1 / no2;
}
}
}
For Global.asax.cs,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace Prac9__Materials_
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
Application["exception"] = ex;
Application["location"] = Request.Url.ToString();
Server.ClearError();
Response.Redirect("ErrorPages/GeneralError.aspx", true);
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
For ErrorPages/GeneralError.aspx,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GeneralError.aspx.cs" Inherits="GeneralError" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
For ErrorPages/GeneralError.aspx.cs,
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Diagnostics;
public partial class GeneralError : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Load_Error(Server.GetLastError());
}
}
protected void Load_Error(Exception objError)
{
if(objError != null)
{
Exception ex = (Exception)Application["ex"];
string FileUrl = (string)Application["location"];
string strError = "<h3>Alert</h3><br /><br />" + "<bold>One error was encountered in " + FileUrl.ToString() + "</bold>" + ex.Message.ToString() + "<br /><br />" + ex.InnerException.ToString();
Response.Write(strError.ToString()); <--- The correct actual output must be displayed on GeneralError.aspx page
Response.Write("<a href='GeneralError.aspx'>" + "GeneralError</a>\n");
Server.ClearError();
}
}
}
For web.config,
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
https:
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.6.1"/>
<httpRuntime targetFramework="4.6.1"/>
<customErrors mode="On" defaultRedirect="ErrorPages/GeneralError.aspx">
<error statusCode="404" redirect="ErrorPages/FileNotFound.htm"/>
</customErrors>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
<system.webServer>
<httpErrors errorMode="Custom" defaultResponseMode="File">
<clear/>
<error statusCode="404" path="ErrorPages\FileNotFound.htm"/>
</httpErrors>
</system.webServer>
</configuration>