'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".'
using System;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
public ViewResult Index()
return View("Index");
}
[HandleError(ExceptionType = typeof(ArgumentOutOfRangeException), View = "HackedUrlWarning")]
[RegExPreconditionFilter("id", PreconditionFilter.ParamType.RouteData, "^[1-9][0-9]*$", typeof(ArgumentOutOfRangeException))]
public ViewResult Company(int id)
return View("Company");
[PredicatePreconditionFilter("id", PreconditionFilter.ParamType.RouteData, "IsGreaterThanZero", typeof(ArgumentOutOfRangeException))]
public ViewResult Employee(int id)
return View("Employee");
protected bool IsGreaterThanZero(object value)
try
int id = Convert.ToInt32(value);
return id > 0;
catch
return false;
Remember Me
Powered by: newtelligence dasBlog 1.8.5223.2
© Copyright 2009, Troy DeMonbreun
E-mail