String Extension Methods in Razor
January 8, 2011 Ely Lucas
The Razor view engine provides helpful extension methods for views that handle type detection and conversion of string values. Here is an example:
@{
if (Request.QueryString["amount"].IsDecimal())
{
@Request.QueryString["amount"].AsDecimal().ToString("c")
}
else
{
@: Amount is not a valid number
}
}
Extension methods exist in pairs: Is(Type) and As(Type) methods for bools, datetimes, decimals, floats, and ints. The As(Type) conversion methods accept an optional second parameter serving as a default value if conversion fails.
These utilities reside in the System.Web.WebPages namespace, which MVC3 and Webmatrix projects reference by default. Developers can add a using statement to System.Web.WebPages to leverage these methods in their views when needed.