# Wednesday, June 04, 2008

Action Precondition Filter

From a previous article I explain that an "action precondition filter" fulfills the following common need in many web applications:

'I often find the need to check for the existence of a parameter passed to a web page, such as an ID, and also often need to check if the ID is valid as well (e.g.: a valid primary key).

In the spirit of Design By Contract, I would consider this check to be a sort of "Request By Contract", at least insofar as the concept of a "precondition".'
For more detail, see my previous article to answer the question: What is an Action Precondition Filter?

What is MVCContrib?

MVCContrib is the contrib project for the ASP.Net MVC Framework. This means it is an Open Source repository for people to submit complementary extending, supporting, and tooling code for the ASP.NET MVC Framework. The project is hosted on CodePlex, and the source code is hosted on Google Code. PreconditionFilter is now available in the MVCContrib code base. Direct links are located below.

Latest Changes to Action Precondition Filter

  • Additionally supports HttpRequest parameters (i.e.: HttpRequest.QueryString, HttpRequest.Form, HttpRequest.ServerVariables, & HttpRequest.Cookies), specified via the ParamType enum.
  • Similarities between PredicatePreconditionFilter & RegExPreconditionFilter have been refactored into an abstract base class to make development of other types of precondition filters easier and more consistent.
  • The ErrorUrl argument has been removed. The same basic result can be accomplished using the RescueAttribute which is also part of MVCContrib. Just add the attribute to the action along with the PreconditionFilter attribute, for example, including [Rescue("Error")] will render the rescue view "Error" for any Exceptions thrown due to an invalid parameter caught by the PreconditionFilter.
  • Exception message is now more descriptive.
  • Verified against ASP.NET Preview 3 release.

Show Me the (latest) Code!

Example Controller (to illustrate usage):

using System;
using System.Web.Mvc;
 
namespace MvcApplication1.Controllers
{
 
    public class HomeController : Controller
    {
 
        public void Index()
        {
            RenderView("Index");
        }
 
        [RegExPreconditionFilter("id", PreconditionFilter.ParamType.RouteData, "^[1-9][0-9]*$", 
typeof(ArgumentOutOfRangeException))]
        public void Company(int id)
        {
            RenderView("Company");
        }
 
        [PredicatePreconditionFilter("id", PreconditionFilter.ParamType.RouteData, 

"IsGreaterThanZero", 
typeof(ArgumentOutOfRangeException))]
        public void Employee(int id)
        {
            RenderView("Employee");
        }
 
        public void Error()
        {
            RenderView("Error");
        }
 
        protected bool IsGreaterThanZero(object value)
        {
            try
            {
                int id = Convert.ToInt32(value);
                return id > 0;
            }
            catch
            {
                return false;
            }
        }
 
    }
 
}

Direct links to Source Code

PreconditionFilter (Abstract Base Class)

PredicatePreconditionFilter

RegExPreconditionFilter

Runnable Usage Example

For a "runnable" usage example, get this release of MVCContrib and load the "/src/Samples/MvcContrib.Samples.ConventionController/MvcContrib.Samples.ConventionController.sln" Visual Studio solution file and run it, clicking on the "Try and access TrackSingle action w/o an 'id' RouteData parameter" link on the index page that loads.
.NET | ASP.NET | C_Sharp | MVC