Friday, July 24, 2009

Simple Error Reporting

This should be a no-brainer, but every ASP.NET application you put out there should have some solid error reporting behind it. The simplest way you can do this is by sending an email message each time there is an error. Nothing like debugging an error before the customer even contacts you about it. Code below.

You'll want to put the following in your Global.asax file:

VB.NET:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

If Request.ServerVariables("server_name") <> "localhost" AndAlso InStr(Request.Url.ToString, "get_aspx_ver.aspx") = 0 AndAlso InStr(Request.Url.ToString.ToLower(), "webresource.axd") = 0 AndAlso InStr(Request.Url.ToString.ToLower(), "scriptresource.axd") = 0 Then

' only run this if it's NOT the localhost and it's a real error - don't care about webresource.axd or scriptresource.axd errors since those don't usually show themselves to the end user

Dim objErr As Exception = Server.GetLastError.GetBaseException

Dim err As String

err = "<p><b>Error caught in Application_Error event:</b></p>"
err = err & "<p>Error URL: " & Request.Url.ToString & "</p>"
err = err & "<p>Error Message: " & objErr.Message.ToString & "</p>"
err = err & "<p>Source: " & objErr.Source.ToString & "</p>"
err = err & "<p>Stack Trace: " & objErr.StackTrace.ToString & "</p>"
err = err & "<p>Client IP Address: " & Request.ServerVariables("REMOTE_ADDR") & "</p>"

Server.ClearError()


Dim objMail As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("your.smtp.server")
Dim mailFrom As System.Net.Mail.MailAddress = New System.Net.Mail.MailAddress("from@somewhere.com", "Error")
Dim mailTo As System.Net.Mail.MailAddress = New System.Net.Mail.MailAddress("to@somewhere.com")
Dim mailMsg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage(mailFrom, mailTo)
mailMsg.Subject = "Error"

mailMsg.Body = err
mailMsg.IsBodyHtml = True
objMail.Send(mailMsg)

' redirect to a friendly error page
Response.Redirect("~/Error.htm")

End If

End Sub


C#:

void Application_Error(object sender, EventArgs e) 
{
// Code that runs when an unhandled error occurs

string url = Request.Url.ToString();

if (Request.ServerVariables["server_name"] != "localhost" && url.IndexOf("get_aspx_ver.aspx", 0) == -1 && url.IndexOf("WebResource.axd", 0) == -1 && url.IndexOf("ScriptResource.axd", 0) == -1)
{
Exception objErr = Server.GetLastError().GetBaseException();

string err = "<p><b>Error caught in Application_Error event:</b></p>";
err += "<p>Error URL: " + Request.Url.ToString() + "</p>";
err += "<p>Error Message: " + objErr.Message.ToString() + "</p>";
err += "<p>Source: " + objErr.Source.ToString() + "</p>";
err += "<p>Stack Trace: " + objErr.StackTrace.ToString() + "</p>";
err += "<p>Client IP Address: " + Request.ServerVariables["remote_addr"].ToString() + "</p>";

Server.ClearError();

' add your own email code here

Response.Redirect("~/Error.htm");
}
}

Thursday, July 23, 2009

Corrupted ViewState after Postback

A common problem with the Web Forms model of ASP.NET is the overuse and bloat of ViewState. Don't get me wrong, I love me some ViewState, but turning it off when you don't need it can really be beneficial. But I digress. What happens when you have a huge page, maybe with a big ol Wizard control, and your ViewState gets out of control? One of the many errors you see may be something along the lines of:

Unable to validate data.

or

Invalid length for a Base-64 char array.

or

Padding is invalid and cannot be removed.

Not fun for your users at all. Lets say you've taken the steps to turn off ViewState for any controls that don't need them. What else can you do? In my opinion, the easiest thing to do is to compress ViewState between postbacks on the page. But how is this done?

1. Create a new class called "Compressor" in your App_Code directory.

VB.NET:

Imports Microsoft.VisualBasic
Imports System.IO
Imports System.IO.Compression

Public Class Compressor

Public Shared Function Compress(ByVal data As Byte()) As Byte()
Dim output As New MemoryStream()
Dim gzip As New GZipStream(output, CompressionMode.Compress, True)
gzip.Write(data, 0, data.Length)
gzip.Close()
Return output.ToArray()
End Function

Public Shared Function Decompress(ByVal data As Byte()) As Byte()
Dim input As New MemoryStream()
input.Write(data, 0, data.Length)
input.Position = 0
Dim gzip As New GZipStream(input, CompressionMode.Decompress, True)
Dim output As New MemoryStream()
Dim buff As Byte() = New Byte(64) {}
Dim read As Integer = -1
read = gzip.Read(buff, 0, buff.Length)
While read > 0
output.Write(buff, 0, read)
read = gzip.Read(buff, 0, buff.Length)
End While
gzip.Close()
Return output.ToArray()
End Function

End Class


C#:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.IO.Compression;

/// <summary>
/// Summary description for Compressor
/// </summary>
public static class Compressor
{
public static byte[] Compress(byte[] data)
{
MemoryStream output = new MemoryStream();
GZipStream gzip = new GZipStream(output, CompressionMode.Compress, true);
gzip.Write(data, 0, data.Length);
gzip.Close();
return output.ToArray();
}

public static byte[] Decompress(byte[] data)
{
MemoryStream input = new MemoryStream();
input.Write(data, 0, data.Length);
input.Position = 0;
GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true);
MemoryStream output = new MemoryStream();
byte[] buff = new byte[64];
int read = -1;
read = gzip.Read(buff, 0, buff.Length);
while (read > 0)
{
output.Write(buff, 0, read);
read = gzip.Read(buff, 0, buff.Length);
}
gzip.Close();
return output.ToArray();
}
}


2. Throw the following methods in the code behind of the offending page(s).

VB.NET:

    Protected Overloads Overrides Function LoadPageStateFromPersistenceMedium() As Object
Dim viewState As String = Request.Form("__VSTATE")
Dim bytes As Byte() = Convert.FromBase64String(viewState)
bytes = Compressor.Decompress(bytes)
Dim formatter As New LosFormatter()
Return formatter.Deserialize(Convert.ToBase64String(bytes))
End Function

Protected Overloads Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)
Dim formatter As New LosFormatter()
Dim writer As New StringWriter()
formatter.Serialize(writer, viewState)
Dim viewStateString As String = writer.ToString()
Dim bytes As Byte() = Convert.FromBase64String(viewStateString)
bytes = Compressor.Compress(bytes)
ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes))
End Sub


C#:

    protected override object LoadPageStateFromPersistenceMedium()
{
string viewState = Request.Form["__VSTATE"];
byte[] bytes = Convert.FromBase64String(viewState);
bytes = Compressor.Decompress(bytes);
LosFormatter formatter = new LosFormatter();
return formatter.Deserialize(Convert.ToBase64String(bytes));
}

protected override void SavePageStateToPersistenceMedium(object viewState)
{
LosFormatter formatter = new LosFormatter();
StringWriter writer = new StringWriter();
formatter.Serialize(writer, viewState);
string viewStateString = writer.ToString();
byte[] bytes = Convert.FromBase64String(viewStateString);
bytes = Compressor.Compress(bytes);
ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
}


3. Done

The net result of this is to compress your ViewState into a hidden field and decompress it when your page is initialized. Very little performance impact too. Pretty slick.

Friday, July 17, 2009

Setting Focus After AJAX Postback

This one really pissed me off. It should be easy to set the focus of a control after a postback in an AJAX Panel, right? In fact, if I searched Bing (you heard me) for "set focus after ajax postback" you'd think I would find this code. Not so much. It only took a week and a day to find this code:

ScriptManager.GetCurrent(this.Page).SetFocus(control);


...where "control" is the name of the control. Granted, this assumes an ASP.NET 3.5 application with a ScriptManager on the page. Done.