Link to home
Start Free TrialLog in
Avatar of no158
no158

asked on

Web.config and working with custom error pages

As far as I can tell the only way to load a custom error 404 page is through the web.config file.

web.config:
<error statusCode="404" redirect="/Page404Redirect.aspx"/>

What I would like to do is be able to load a custom error 404 page and also capture what the user typed in to get that page.

So the user types in http://www.testURL.com/URL_that_doesnt_exist.aspx
I want them to be redirected to the Error 404 page but also be able to capture that URL they typed in so I can better track where traffic is headed. Maybe make the error page a little more tailored to what the user typed in.

Just wondering if this is possible?

Thanks in advance.
Avatar of Espavo
Espavo
Flag of South Africa image

In my Global.asax I have the following that sends me an error e-mail when an error occurs... you may be able to use some of it for your purposes...
One of the things I check for is ReferURL... (Which it seems you are after too...)

BTW, I bouce all my Errors back to Default.aspx... so from there it would be relatively easy to do custom redirects... (or error messages...)
 

   Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
        
        Dim ReferURL As String 'Try get the Referring URL that generated the error
        Try
            ReferURL = "http://" & Request.UrlReferrer.Host & Request.UrlReferrer.PathAndQuery
        Catch
            ReferURL = "Direct Call"
        End Try
        Dim ex As Exception = Server.GetLastError().GetBaseException()
 
        Dim MailBody As String = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN""><html><body><font face=arial size=2>"
        MailBody &= "<b><u>Referring URL</u></b> :<br>&nbsp;<br><a href=""" & ReferURL & """>" & ReferURL & "</a><br>&nbsp;<br>"
        MailBody &= "<b><u>Message</u></b> :<br>&nbsp;<br>" & ex.Message.ToString & "<br>&nbsp;<br>"
        If Request.Form.ToString() <> "" Then MailBody &= "<b><u>Form</u></b> :<br>&nbsp;<br>" & Request.Form.ToString() & "<br>&nbsp;<br>"
        If Request.QueryString.ToString() <> "" Then MailBody &= "<b><u>QueryString</u></b> :<br>&nbsp;<br>" & Request.QueryString.ToString() & "<br>&nbsp;<br>"
        MailBody &= "<b><u>Source</u></b> :<br>&nbsp;<br>" & ex.Source.ToString & "<br>&nbsp;<br>"
        MailBody &= "<b><u>TargetSite</u></b> :<br>&nbsp;<br>" & ex.TargetSite.ToString & "<br>&nbsp;<br>"
        MailBody &= "<b><u>StackTrace</u></b> :<br>&nbsp;<br>" & ex.StackTrace.ToString & "<br>&nbsp;<br>"
        MailBody &= "</font></body></html>"
 
        Dim mm As New System.Net.Mail.MailMessage()
        mm.From = New System.Net.Mail.MailAddress(ConfigurationManager.AppSettings("ErrorMail:From"), ConfigurationManager.AppSettings("ErrorMail:FromName"))
        mm.To.Add(ConfigurationManager.AppSettings("ErrorMail:To"))
        mm.Subject = ConfigurationManager.AppSettings("ErrorMail:Subject")
        mm.Body = MailBody
        mm.IsBodyHtml = True
 
        Dim smtp As New System.Net.Mail.SmtpClient
        smtp.Send(mm)
    End Sub

Open in new window

Avatar of no158
no158

ASKER

Would you happen to have that code in C#?

    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
    }
ASKER CERTIFIED SOLUTION
Avatar of Espavo
Espavo
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of no158

ASKER

With some minor tweaks to the code I got it to compile. I also changed the mail API since this one works better with our server, we know this because of other web applications and little problems we experienced along the way. I also stripped out the calls to the web.config since we don't have our variables setup that way. I do appreciate the professional touch though ;)

The problem I seem to be running into is that the emails are not being sent. I tried to remove the custom page in order to trigger this function but it does not seem to do the trick. Can you tell me how the Global.asax works and what I must do in order to make that function call run. I was under the assumption it would run before any of the custom pages would.
void Application_Error(object sender, EventArgs e) 
{ 
// Code that runs when an unhandled error occurs
    
string ReferURL = null;
 
//Try get the Referring URL that generated the error
    try {
        ReferURL = "http://" + Request.UrlReferrer.Host + Request.UrlReferrer.PathAndQuery;
    }
    catch {
        ReferURL = "Direct Call";
    }
    Exception ex = Server.GetLastError().GetBaseException();
 
    string MailBody = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html><body><font face=arial size=2>";
    MailBody += "<b><u>Referring URL</u></b> :<br> <br><a href=\"" + ReferURL + "\">" + ReferURL + "</a><br> <br>";
    MailBody += "<b><u>Message</u></b> :<br> <br>" + ex.Message.ToString() + "<br> <br>";
    if (!string.IsNullOrEmpty(Request.Form.ToString())) MailBody += "<b><u>Form</u></b> :<br> <br>" + 
 
Request.Form.ToString() + "<br> <br>"; 
    if (!string.IsNullOrEmpty(Request.QueryString.ToString())) MailBody += "<b><u>QueryString</u></b> 
 
:<br> <br>" + Request.QueryString.ToString() + "<br> <br>"; 
    MailBody += "<b><u>Source</u></b> :<br> <br>" + ex.Source.ToString() + "<br> <br>";
    MailBody += "<b><u>TargetSite</u></b> :<br> <br>" + ex.TargetSite.ToString() + "<br> <br>";
    MailBody += "<b><u>StackTrace</u></b> :<br> <br>" + ex.StackTrace.ToString() + "<br> <br>";
    MailBody += "</font></body></html>";
    
 
System.Web.Mail.MailMessage Mailer = new System.Web.Mail.MailMessage();
Mailer.From = "ALERT@DOMAIN.COM";
Mailer.To = "USERNAME@DOMAIN.com";
Mailer.Subject = "Web Alert - General Error";
//Mailer.BodyFormat = MailFormat.Html;
Mailer.Body = MailBody;
System.Web.Mail.SmtpMail.Send(Mailer);
}

Open in new window

Avatar of no158

ASKER

FYI

The html format didn't seem to compile so I removed it. The mail should still go through even with the html code in the strings, it will just display it as text.
To start with, you said:
As far as I can tell the only way to load a custom error 404 page is through the web.config file.
This not 100% correct... ASP.Net will handle "ASP" errors and do the redirecting based on your web.config... if the error is a "non-ASP.Net" error than IIS will handle the error... (.htm page not found, for example)
Something that I have done is I've created a "404Redirector" that picks-up "htm not-found" errors, and redirects them to a specific .aspx page that can then handle output... (This is a great way for making dynamic sites SEO friendly...)
You need to configure IIS Custom Error pages (404) to use the
Action: "URL"
Value: "/404Redirect.aspx" as the page to direct to...
Here's the VB code... (and the converted C#)
(Converted at http://converter.telerik.com/)

VB
==
 
Imports System.Data.SqlClient
 
Partial Class _404Redirect
    Inherits System.Web.UI.Page
    Public defaultURL, DefaultPage As String
    Private conn0 As New SqlConnection(ConfigurationManager.ConnectionStrings("connSQL").ConnectionString)
 
 
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DefaultPage = ConfigurationManager.AppSettings("DefaultPage")
        Dim HTTP_HOST As String = "http://" & LCase(Request.ServerVariables("HTTP_HOST")) & "/"
        If HTTP_HOST Like "*glp*" Then
            HTTP_HOST &= "CProp/"
        End If
 
        defaultURL = HTTP_HOST & DefaultPage
 
        Dim Get404 As System.Collections.Specialized.NameValueCollection
        Dim chk404 As String
 
        Get404 = Request.QueryString
        chk404 = LCase(Server.UrlDecode(Get404.ToString))
        'chk404 = Server.UrlDecode(Get404.ToString)
 
        Dim SplitURL As Array = Array.CreateInstance(GetType(String), 0)
        Try
            SplitURL = Split(chk404, "/")
        Catch
            Response.Redirect(defaultURL)
        End Try
        chk404 = SplitURL(SplitURL.GetUpperBound(0))
 
        Dim whole404 = LCase(Server.UrlDecode(Get404.ToString))
 
        'Get the PageName (The piece in front of the .htm)
        'If InStr(chk404, ".") > 0 Then chk404 = Left(chk404, (InStr(chk404, ".") - 1))
 
        If chk404 <> "" Then
 
            If whole404.ToLower.Contains("getfile") Then
                Server.Transfer("getFile.aspx?mediaId=" & GetFileId(SplitURL(SplitURL.GetUpperBound(0))))
            ElseIf whole404.ToLower.Contains("ssdata") Then
                GetSSData(SplitURL(SplitURL.GetUpperBound(0)))
            ElseIf whole404.ToLower.Contains("sspic") Then
                GetSSPic(SplitURL(SplitURL.GetUpperBound(0)))
            ElseIf whole404.ToLower.Contains("ssvid") Then
                GetSSVideo(SplitURL(SplitURL.GetUpperBound(0)))
            ElseIf whole404.ToLower.Contains("sstvid") Then
                GetSSPicT(SplitURL(SplitURL.GetUpperBound(0)))
            ElseIf whole404.ToLower.Contains("getthumbnail") Then
                Server.Transfer("pic.aspx?i=" & GetThumbnailId(SplitURL(SplitURL.GetUpperBound(0))) & "&h=" & SplitURL(SplitURL.GetUpperBound(0) - 1) & "&w=" & SplitURL(SplitURL.GetUpperBound(0) - 2) & "&tbl=MediaThumbnails&col=ThumbnailId")
            ElseIf whole404.ToLower.Contains("getimage") Then
                Server.Transfer("picG.aspx?i=" & GetImageId(SplitURL(SplitURL.GetUpperBound(0))) & "&h=" & SplitURL(SplitURL.GetUpperBound(0) - 1) & "&w=" & SplitURL(SplitURL.GetUpperBound(0) - 2))
            Else
                Dim cmd As New SqlCommand("SELECT PageID FROM Pages WHERE PageFilename = '" & chk404 & "'", conn0)
                conn0.Open() : Dim PageID As Integer = cmd.ExecuteScalar() : conn0.Close() : cmd.Dispose()
 
                If PageID > 0 Then
                    Dim cmd0 As New SqlCommand("SELECT uID FROM SiteMap WHERE (PageID = " & PageID & ") ORDER BY ID DESC", conn0)
                    conn0.Open() : Dim uID As Integer = cmd0.ExecuteScalar() : conn0.Close() : cmd.Dispose()
                    'If InStr(chk404, ".") > 0 Then chk404 = Left(chk404, (InStr(chk404, ".") - 1))
                    'Response.Redirect("default.aspx?GetPage=" & chk404 & "&MenuID=" & uID)
                    Server.Transfer("default.aspx?GetPage=" & chk404 & "&MenuID=" & uID)
                Else
                    Response.Redirect(defaultURL)
                End If
            End If
 
 
 
        Else
            Response.Redirect(defaultURL)
        End If
 
    End Sub
 
    Private Function GetFileId(ByVal filename404 As String) As String
        filename404 = Replace(filename404, "getfile", "")
        If filename404.Contains(".") = True Then
            'Return (Right(filename404, filename404.Length - filename404.LastIndexOf(".") - 1))
            Return Left(filename404, filename404.LastIndexOf("."))
        Else
            Return ""
        End If
    End Function
 
    Private Function GetImageId(ByVal filename404 As String) As String
        filename404 = Replace(filename404, "getimage", "")
        If filename404.Contains(".") = True Then
            'Return (Right(filename404, filename404.Length - filename404.LastIndexOf(".") - 1))
            Return Left(filename404, filename404.LastIndexOf("."))
        Else
            Return ""
        End If
    End Function
 
    Private Function GetThumbnailId(ByVal filename404 As String) As String
        filename404 = Replace(filename404, "getthumbnail", "")
        If filename404.Contains(".") = True Then
            'Return (Right(filename404, filename404.Length - filename404.LastIndexOf(".") - 1))
            Return Left(filename404, filename404.LastIndexOf("."))
        Else
            Return ""
        End If
    End Function
 
    Private Sub GetSSData(ByVal filename404 As String)
        'The Image Name is made-up of SSPic_i_w_h.jpg
        Dim SplitFName As Array = Array.CreateInstance(GetType(String), 0)
        SplitFName = Split(Left(filename404, filename404.LastIndexOf(".")), "_")
 
        Server.Transfer("~/SlideShow/SSData.aspx?pr=" & SplitFName(SplitFName.GetUpperBound(0)))
    End Sub
 
    Private Sub GetSSPic(ByVal filename404 As String)
        'The Image Name is made-up of SSPic_i_w_h.jpg
        Dim SplitFName As Array = Array.CreateInstance(GetType(String), 0)
        SplitFName = Split(Left(filename404, filename404.LastIndexOf(".")), "_")
 
        Server.Transfer("~/picG.aspx?i=" & SplitFName(SplitFName.GetUpperBound(0) - 2) & "&w=" & SplitFName(SplitFName.GetUpperBound(0) - 1) & "&h=" & SplitFName(SplitFName.GetUpperBound(0)))
    End Sub
 
    Private Sub GetSSVideo(ByVal filename404 As String)
        'The Image Name is made-up of SSPic_i_w_h.jpg
        Dim SplitFName As Array = Array.CreateInstance(GetType(String), 0)
        SplitFName = Split(Left(filename404, filename404.LastIndexOf(".")), "_")
 
        Server.Transfer("~/getFile.aspx?mediaId=" & SplitFName(SplitFName.GetUpperBound(0)))
    End Sub
 
    Private Sub GetSSPicT(ByVal filename404 As String)
        'The Image Name is made-up of SSPic_i_w_h.jpg
        Dim SplitFName As Array = Array.CreateInstance(GetType(String), 0)
        SplitFName = Split(Left(filename404, filename404.LastIndexOf(".")), "_")
 
        Response.Redirect("~/picT.aspx?i=" & SplitFName(SplitFName.GetUpperBound(0) - 2) & "&w=" & SplitFName(SplitFName.GetUpperBound(0) - 1) & "&h=" & SplitFName(SplitFName.GetUpperBound(0)))
    End Sub
End Class
 
 
C#
==
 
using System.Data.SqlClient;
 
partial class _404Redirect : System.Web.UI.Page
{
	public string defaultURL;
	public string DefaultPage;
	private SqlConnection conn0 = new SqlConnection(ConfigurationManager.ConnectionStrings("connSQL").ConnectionString);
 
 
	private void  // ERROR: Handles clauses are not supported in C#
Page_Load(System.Object sender, System.EventArgs e)
	{
		DefaultPage = ConfigurationManager.AppSettings("DefaultPage");
		string HTTP_HOST = "http://" + LCase(Request.ServerVariables("HTTP_HOST")) + "/";
		if (HTTP_HOST // ERROR: Unknown binary operator Like
) {
			HTTP_HOST += "CProp/";
		}
 
		defaultURL = HTTP_HOST + DefaultPage;
 
		System.Collections.Specialized.NameValueCollection Get404;
		string chk404;
 
		Get404 = Request.QueryString;
		chk404 = LCase(Server.UrlDecode(Get404.ToString));
		//chk404 = Server.UrlDecode(Get404.ToString)
 
		Array SplitURL = Array.CreateInstance(typeof(string), 0);
		try {
			SplitURL = Split(chk404, "/");
		}
		catch {
			Response.Redirect(defaultURL);
		}
		chk404 = SplitURL(SplitURL.GetUpperBound(0));
 
		object whole404 = LCase(Server.UrlDecode(Get404.ToString));
 
		//Get the PageName (The piece in front of the .htm)
		//If InStr(chk404, ".") > 0 Then chk404 = Left(chk404, (InStr(chk404, ".") - 1))
 
		if (chk404 != "") {
 
			if (whole404.ToLower.Contains("getfile")) {
				Server.Transfer("getFile.aspx?mediaId=" + GetFileId(SplitURL(SplitURL.GetUpperBound(0))));
			}
			else if (whole404.ToLower.Contains("ssdata")) {
				GetSSData(SplitURL(SplitURL.GetUpperBound(0)));
			}
			else if (whole404.ToLower.Contains("sspic")) {
				GetSSPic(SplitURL(SplitURL.GetUpperBound(0)));
			}
			else if (whole404.ToLower.Contains("ssvid")) {
				GetSSVideo(SplitURL(SplitURL.GetUpperBound(0)));
			}
			else if (whole404.ToLower.Contains("sstvid")) {
				GetSSPicT(SplitURL(SplitURL.GetUpperBound(0)));
			}
			else if (whole404.ToLower.Contains("getthumbnail")) {
				Server.Transfer("pic.aspx?i=" + GetThumbnailId(SplitURL(SplitURL.GetUpperBound(0))) + "&h=" + SplitURL(SplitURL.GetUpperBound(0) - 1) + "&w=" + SplitURL(SplitURL.GetUpperBound(0) - 2) + "&tbl=MediaThumbnails&col=ThumbnailId");
			}
			else if (whole404.ToLower.Contains("getimage")) {
				Server.Transfer("picG.aspx?i=" + GetImageId(SplitURL(SplitURL.GetUpperBound(0))) + "&h=" + SplitURL(SplitURL.GetUpperBound(0) - 1) + "&w=" + SplitURL(SplitURL.GetUpperBound(0) - 2));
			}
			else {
				SqlCommand cmd = new SqlCommand("SELECT PageID FROM Pages WHERE PageFilename = '" + chk404 + "'", conn0);
				conn0.Open();
				int PageID = cmd.ExecuteScalar();
				conn0.Close();
				cmd.Dispose();
 
				if (PageID > 0) {
					SqlCommand cmd0 = new SqlCommand("SELECT uID FROM SiteMap WHERE (PageID = " + PageID + ") ORDER BY ID DESC", conn0);
					conn0.Open();
					int uID = cmd0.ExecuteScalar();
					conn0.Close();
					cmd.Dispose();
					//If InStr(chk404, ".") > 0 Then chk404 = Left(chk404, (InStr(chk404, ".") - 1))
					//Response.Redirect("default.aspx?GetPage=" & chk404 & "&MenuID=" & uID)
					Server.Transfer("default.aspx?GetPage=" + chk404 + "&MenuID=" + uID);
				}
				else {
					Response.Redirect(defaultURL);
				}
			}
		}
 
 
 
		else {
			Response.Redirect(defaultURL);
		}
 
	}
 
	private string GetFileId(string filename404)
	{
		filename404 = Replace(filename404, "getfile", "");
		if (filename404.Contains(".") == true) {
			//Return (Right(filename404, filename404.Length - filename404.LastIndexOf(".") - 1))
			return Left(filename404, filename404.LastIndexOf("."));
		}
		else {
			return "";
		}
	}
 
	private string GetImageId(string filename404)
	{
		filename404 = Replace(filename404, "getimage", "");
		if (filename404.Contains(".") == true) {
			//Return (Right(filename404, filename404.Length - filename404.LastIndexOf(".") - 1))
			return Left(filename404, filename404.LastIndexOf("."));
		}
		else {
			return "";
		}
	}
 
	private string GetThumbnailId(string filename404)
	{
		filename404 = Replace(filename404, "getthumbnail", "");
		if (filename404.Contains(".") == true) {
			//Return (Right(filename404, filename404.Length - filename404.LastIndexOf(".") - 1))
			return Left(filename404, filename404.LastIndexOf("."));
		}
		else {
			return "";
		}
	}
 
	private void GetSSData(string filename404)
	{
		//The Image Name is made-up of SSPic_i_w_h.jpg
		Array SplitFName = Array.CreateInstance(typeof(string), 0);
		SplitFName = Split(Left(filename404, filename404.LastIndexOf(".")), "_");
 
		Server.Transfer("~/SlideShow/SSData.aspx?pr=" + SplitFName(SplitFName.GetUpperBound(0)));
	}
 
	private void GetSSPic(string filename404)
	{
		//The Image Name is made-up of SSPic_i_w_h.jpg
		Array SplitFName = Array.CreateInstance(typeof(string), 0);
		SplitFName = Split(Left(filename404, filename404.LastIndexOf(".")), "_");
 
		Server.Transfer("~/picG.aspx?i=" + SplitFName(SplitFName.GetUpperBound(0) - 2) + "&w=" + SplitFName(SplitFName.GetUpperBound(0) - 1) + "&h=" + SplitFName(SplitFName.GetUpperBound(0)));
	}
 
	private void GetSSVideo(string filename404)
	{
		//The Image Name is made-up of SSPic_i_w_h.jpg
		Array SplitFName = Array.CreateInstance(typeof(string), 0);
		SplitFName = Split(Left(filename404, filename404.LastIndexOf(".")), "_");
 
		Server.Transfer("~/getFile.aspx?mediaId=" + SplitFName(SplitFName.GetUpperBound(0)));
	}
 
	private void GetSSPicT(string filename404)
	{
		//The Image Name is made-up of SSPic_i_w_h.jpg
		Array SplitFName = Array.CreateInstance(typeof(string), 0);
		SplitFName = Split(Left(filename404, filename404.LastIndexOf(".")), "_");
 
		Response.Redirect("~/picT.aspx?i=" + SplitFName(SplitFName.GetUpperBound(0) - 2) + "&w=" + SplitFName(SplitFName.GetUpperBound(0) - 1) + "&h=" + SplitFName(SplitFName.GetUpperBound(0)));
	}
}

Open in new window

Avatar of no158

ASKER

As far as I know the Global.asax page will load when an application-level event is raised by ASP.NET. So a 404 should trigger the "void Application_Error(object sender, EventArgs e)" function. Yet I'm not getting any emails so something is not working here. I noticed that the Global.asax file does not define any APIs at the top, will this affect the mail functions?
Only an ASPX 404 Error will will load the application-level event sub in Global.asax... (So if you were looking for TestPage.aspx and it doesn't exist then the sub will be triggered... if the file is TestPage.htm that you are looking for, and it doesn't exist, then IIS will handle the error...)

Note that ALL ASPX errors trigger the Sub in Global.asax...
API's don't need to be specified at the top IF they are included in the code itself... which is the case here...
Avatar of no158

ASKER

The picture is becoming much clearer.

So just entering in the code in the Application_Error(object sender, EventArgs e) will be sufficient to send the emails, as long as there is an ASP 404 Error?

If this is true I don't understand why I'm not getting emails, any ideas?
Avatar of no158

ASKER

I tried http://localhost/pageThatDoesntExist.aspx and I get the server applicatoin error but no email.
Are you able to send yourself an e-mail from a "normal" .aspx page?

If yes, how is that code different to what's in the Global.asax page?
Just thinking... do you have the mailsettings in your Web.Config?
<system.net>
<mailSettings>
<smtp from="webmaster@somedomain.com">
<network host="mail.somedomeain.com" password="password" userName="apps@somedomain.com"/>
</smtp>
</mailSettings>
</system.net>
Avatar of no158

ASKER

The email code is exactly the same as another application that is currently running on the site and I just tested it, it's working as intended.

No, I don't have my email settings listed in the web.config.
Have you tried putting a Break-Point in the code, and debugging it?
Avatar of no158

ASKER

Thanks so much for your help.

The reason it wasn't working is because I was working on a beta box, which does not have an smtp setup.

Silly me...

Thanks again, HUGE HELP :)