'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 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;
Remember Me
Powered by: newtelligence dasBlog 1.8.5223.2
© Copyright 2008, Troy DeMonbreun
E-mail