Logging handled exceptions with ELFAR for ASP.NET MVC
Friday, May 11, 2012 4:42 PM

Erik Funkenbusch posted a question, on the ELFAR discussion board, regarding the logging of handled exceptions. He managed to figure out a solution and kindly posted it:

FYI, I found a solution that works pretty well.

try 
{
throw new NotImplementedException();
}
catch (Exception e)
{
foreach (var filter in GlobalFilters.Filters)
{
if (filter.Instance is ErrorLogFilter)
{
var f = filter.Instance as ErrorLogFilter;
f.OnException(new ExceptionContext(ControllerContext, e));
}
}
}

I actually took this one step further and created a .Log() extension method on Exception.

public static class ExceptionExtensions 
{
public static void Log(this Exception theException, ControllerContext theContext)
{
foreach (var filter in GlobalFilters.Filters)
{
if (filter.Instance is ErrorLogFilter)
{
var f = filter.Instance as ErrorLogFilter;
f.OnException(new ExceptionContext(theContext, theException));
}
}
}
}

Then you just need to use it as such:

try { 
throw new NotImplementedException();
}
catch (Exception e)
{
[...]
e.Log(ControllerContext);
}

However, there is an alternative solution: throw the exception and let ELFAR automatically handle it:

try { 
throw new NotImplementedException();
}
catch (Exception e)
{
...
throw;
}
Figure 1

Update:

Erik Funkenbusch responded with some good points:

Rethrowing the exception will result in an unhandle exception propogating to the client...The whole point is to handle th[e] exception, but log it so it can be investigated. I find this particularly useful with exceptions for external resources (Web requests, file access, etc..). There is no point in passing this on to the user if you can continue.

Also, I read your blog and I think you should just use throw, rather than throw e. If you use throw e it will lose the stack trace.

I've updated the code accordingly.

JavaScript QueryString
Friday, March 30, 2012 12:53 PM

Yesterday I needed to access a querystring parameter, from JavaScript. There is no in-built object or property but there are plenty of examples of how to do this. However, I've written a neat self executing function (Figure 1) that gets the key/value pairs, for each querystring parameter, and creates a querystring property on the window.location object.

(function() {
var querystring = {};
window.location.search.replace(
/([^?=&]+)(=([^&]*))?/g,
function($0, $1, $2, $3) { querystring[$1] = $3; });
window.location.querystring = querystring;
})();
Figure 1

Introducing Error Logging Filter and Route (ELFAR) for ASP.NET MVC
Thursday, February 23, 2012 3:54 PM

Introduction

Error Logging Filter and Route (ELFAR) for ASP.NET MVC was inspired by and based on the popular Error Logging Modules And Handlers (ELMAH) for ASP.NET utility. ELFAR retains the familiar ELMAH user interface but has been built to utilise the architecture of ASP.NET MVC. Unlike ELMAH, ELFAR is not driven by configuration and can be added to your website/web application with just 3 lines of code (usually in the Application_Start method):

var provider = new ErrorLogProvider();
GlobalFilters.Filters.Add(new ErrorLogFilter(provider));
RouteTable.Routes.Insert(0, new ErrorLogRoute(provider));
Figure 1

To access ELFAR, navigate to ~/elfar (where ~/ is the root directory of the MVC application).

ELFAR has 6 out-of-the-box error log providers (available on NuGet):

Features

Once ELFAR has been added to your website/web application, you get the following features:

  • a web page to view a list of error logs
  • a web page to view the details of an error log, including a coloured stack trace
  • a view of the original yellow screen of death (where appropriate)
  • XML and JSON downloads of the error log
  • an RSS feed of the last 15 error logs
  • an RSS digest of daily error logs (limited to 900)
  • a CSV download of all error logs

Optional features

In addition, there are several optional features available:

  • Excluding unwanted exceptions
  • Restricting access to the ELFAR web pages
  • Email notification of errors
  • Posting errors to Twitter

Resources

Further information can be found in the ELFAR Wiki.

Full source for ELFAR can be found at SourceForge.net

Detecting Page Refresh in ASP.NET WebForms Redux
Saturday, February 18, 2012 3:36 PM

This is an article that I wrote on 8th November 2004, which I later removed from my website.  Fortunately, it had been copied verbatim and translated into VB.NET (although neither of them acknowledge that it's my article), so I thought I'd add it to my new blog (with some minor code adjustments).

Introduction

A common problem that Web Application Developers encounter is how to stop the user from refreshing the page. This is a problem if the previous request to the server was a PostBack, which, for example, inserted the WebForm’s data into a database. The result: duplicate rows in the database. The answer to this problem is that you can’t stop the user from refreshing the page, however there is a way to determine if this event has occurred.

In his article “Build Your ASP.NET Pages on a Richer Bedrock” [no longer available] Dino Esposito outlined a mechanism to detect a page refresh. This method is cumbersome and more complicated than necessary, although the fundamental idea is sound and forms the basis of this solution. Dino’s mechanism uses a counter stored on the page and a session variable to store the previous request’s counter on the server, if the two match then we have a page refresh.

A simpler method

Keeping the whole process within a base Page class ensures that the mechanism is completely encapsulated (and simple to implement) and if we use ViewState we eliminate the need to use an additional hidden field. Also, as we simply want to test if the two storage devices contain the same value, we can use two boolean variables, which further simplifies the process.

The last decision to make is where, in the Page’s lifecycle, should the process take place. As we are using ViewState it would seem logical to perform the operation in the LoadViewState and SaveViewState methods. Using these two methods, and not the OnLoad method, has further benefits in that it eliminates potential problems with derived classes implementing Page_Load.

How the process works

The LoadViewState method, which is part of the Page’s initialisation phase, is only invoked during PostBack and therefore SaveViewState is the only method, of the two ViewState related methods, to be called when the page is first requested.

protected override object SaveViewState()
{
    SessionState = RefreshState;
    var = new object[2];
    allStates[0] = base.SaveViewState();
    allStates[1] = !RefreshState;
    return allStates;
}
Figure 1

Note: RefreshState (which on initial page request is defaulted to false and on subsequent PostBack requests is the value of ViewState) is assigned to the SessionState item and the negated RefreshState is saved to the new ViewState.

Once a PostBack event takes place the LoadViewState method is called.

protected override void LoadViewState(object savedState)
{
    var = (object[]) savedState;
    base.LoadViewState(allStates[0]);
    RefreshState = (bool) allStates[1];
    IsRefresh = RefreshState == SessionState;
}
Figure 2

Note: The RefreshState is retrieved from ViewState and compared with the value in the SessionState. The result is stored in IsRefresh Property.

The listing below shows the entire class definition:

namespace StevenBey.Web.UI
{
    public class Page
        : System.Web.UI.Page
    {
        protected override void LoadViewState(object savedState)
        {
            var = (object[]) savedState;
            base.LoadViewState(allStates[0]);
            RefreshState = (bool) allStates[1];
            IsRefresh = RefreshState == SessionState;
        }

        protected override object SaveViewState()
        {
            SessionState = RefreshState;
            var = new object[2];
            allStates[0] = base.SaveViewState();
            allStates[1] = !RefreshState;
            return allStates;
        }

        public bool IsRefresh { get; private set; }

        bool RefreshState { get; set; }
        bool SessionState
        {
            get { return (bool) Session["__ISREFRESH"]; }
            set { Session["__ISREFRESH"] = value; }
        }
    }
}
Figure 3

Testing the process

<%@ Page Inherits="StevenBey.Web.UI.Page" %>
<html>
    <head>
        <title>Detecting Page Refresh</title>
    </head>
    <body>
        IsRefresh = <%= IsRefresh %>
        <form runat="server">
            <asp:Button runat="server" Text="Submit" />
        </form>
    </body>
</html>
Figure 4

Clicking on the button invokes a PostBack, however the value of IsRefresh doesn’t change until you click on the browser’s Refresh button or press F5 on the keyboard (and then click “Retry”). Clicking on the button once again resets the value of IsRefresh to false.

Live Demo

Conclusion

In this article I have demonstrated a simplified method of detecting a page refresh event.

Download the files used in this this article.

Enable multiple submit buttons in ASP.NET MVC
Saturday, February 18, 2012 12:00 AM

Introduction

Recently, I started working on a large eCommerce website, implemented in ASP.NET MVC 3. I soon found that many of the forms in the site contained code similar to Figure 1, which I took an instant dislike to. The reason I dislike it is that “Action” doesn’t relate to anything (i.e. it isn’t an actual action name) and causes the URL to change when the form is submitted, so a URL like “/Blog/AddPost” will suddenly become “/Blog/Action”. Another side-effect is that the destination action method must specify explicitly the view name. I then discovered that it was to enable multiple submit buttons by using an implementation of the ActionNameSelectorAttribute class (Figure 2).

@using(Html.BeginForm("Action"))
{


}
Figure 1
public class SubmitButtonNameActionNameSelector
: ActionNameSelectorAttribute
{
public override bool IsValidName(
ControllerContext controllerContext,
string actionName,
MethodInfo methodInfo)
{
if(actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
return true;

if(!actionName.Equals("Action", StringComparison.InvariantCultureIgnoreCase))
return false;

var request = controllerContext.RequestContext.HttpContext.Request;
return request[methodInfo.Name] != null;
}
}
Figure 2
public class BlogController
: Controller
{
[SubmitButtonNameActionNameSelector]
public ActionResult Save(...)
{
...
}
[SubmitButtonNameActionNameSelector]
public ActionResult Publish(...)
{
...
}
}
Figure 3

Previous technique

In other projects I’ve used a popular technique of binding the button’s name to a parameter name in the action method and then determining the button’s value, in order to branch to a different method. There are downsides to this approach but the main thing I dislike about it is that it’s quite messy. The ActionNameSelectorAttribute implementation allows for a cleaner separation of behaviour for each button.

Solving the problem

To circumvent the problems I have mentioned, I decided to use a variation of the SubmitButtonNameActionNameSelector implementation (Figure 4).

public class FormActionAttribute
: ActionNameSelectorAttribute
{
public override bool IsValidName(
ControllerContext controllerContext,
string actionName,
MethodInfo methodInfo)
{
return controllerContext.HttpContext.Request[Prefix + methodInfo.Name] != null
&& !controllerContext.IsChildAction;
}

public string Prefix = "Action::";
}
Figure 4
public class BlogController
: Controller
{
[FormAction]
public ActionResult Save(...)
{
...
}
[FormAction(Prefix = "Action_")]
public ActionResult Publish(...)
{
...
}
}
Figure 5
@using(Html.BeginForm())
{


}
Figure 6

Conclusion

By prefixing the button’s name there is no need to have “Action” as the form’s action (Figure 6), which means that the URL stays the same and there is no need to explicitly specify the view name.