I got an email this afternoon from someone who is coming up to speed with WebMatrix and needed some functionality that they couldn’t find in any of the helpers in the gallery.
It is easy to forget that ALL OF .NET is still there at our disposal when programming with WebMatrix / ASP.NET Web Pages.
Using functionality that exists in the .NET Framework is easy.
All we need to do is pull in a name space just like we would in any other ASP.NET page.
Suppose we want to use the Regular Expression classes from .NET.
First, at the top of a .cshtml page we place a “using” statement and specify the name-space that we want to access.
Layout = "~/Shared/_SiteLayout.cshtml";
Page.Title = "We Look Forward to Hearing from you!";
Then we can create a .NET “Regex” object passing in the expression that we want to validate against and then call the “IsMatch” method
var isValidEmail = true;
var pattern = @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*
var regExp = new Regex(pattern);
isValidEmail = regExp.IsMatch(contactemail);
if(isValidEmail == false)
validationerrors = true;
@:ERROR : "Contact Email" Is Required –
You must enter a VALID Email Address.<br />
I’ll be building a sample application using WebMatrix and implementing a number of features that use the .NET framework, but this sample should illustrate how easy is is to get under the WebMatrix covers to .NET !