Tuesday, August 21, 2012

C# simple mailto: link set to return value of a method

 

Quick Description:  A quick example of some methods to build a dynamic mailto: string for c#

I know mailto: links are not exactly the future, but for my purposes, they're very handy for handling things like adding some error information context to the "Click here to get help with this error" button everyone (right?) puts on their custom error page.  .Net has its own methods for messaging if you want to get emails in the background, but for things like error pages, it's nice not to have to assume the application is in a happy state if a user wants to tell you more about the ugly thing that just happened (hopefully it doesn't happen often, and obviously, the system logging happens in the background).

So, that being said, using the asp:HyperLink allows you to use its server side methods.    Below is a simple example of adding exception information

 
//do this work on the pre-render event of the hyperlink object.
protected void HyperLinkMailToMe_PreRender(Exception ApplicationException)
{
string strMessageBody = CheckNullExceptionInformation(CurrentException.Message, "Exception Message");
string strErrorID = "if you give errors unique ids";
string strMailtoLink = "mailto:myemailaddress?subject=Information regarding error " + strErrorID + "?Body=" + strMessageBody
HyperLinkMailToMe.NavigateUrl = strMailtoLink;
}
//check that the exception property is not null
public static string CheckNullExceptionInformation(object ExceptionProperty, string ExceptionPropertyLabel)
{
if (ExceptionProperty == null)
{
return ExceptionPropertyLabel + " contains no information";
}
else
{
return ExceptionProperty.ToString();
}
}

No comments:

Post a Comment