Razor Engine with Web matrix

Dheeraj Kumar Gunti

Reading Time : ( words)

WebMatrix is a task-focused tool that is designed to make it really easy to get started with web development. It minimizes the number of concepts someone needs to learn in order to get simple things done, and includes and integrates all of the pieces necessary to quickly build Web sites. WebMatrix is a bundle of software running on the developer's machine, with the aim of simplifying the process of web application development using Windows. The bundle includes the IIS Express web server, the SQL Server Compact database engine and the ASP.NET Web Pages framework.

The ASP.NET Web Pages framework contains the 'Razor' syntax for inline coding of C# and VB within HTML.

WebMatrix claims to simplify the website development process because it:

  • Is a relatively small, free download
  • Allows code to be written using ASP.NET, 'Razor', or PHP
  • Supports HTML5
  • Integrates a web server (IIS Express) and a database engine (SQL Server Compact)
  • Installs and configures components such as PHP or MySQL for Windows users
  • Integrates deployment via FTP or WebDeploy
  • Allows deployment to LAMP-based servers as well as Windows-based servers
  • Includes Search Engine Optimization (SEO) tools
  • Following are the few methods used,
Database.Open Method

Opens a connection to a database using the specified file name or using the named connection string. The name associated with the database to open. name can specify an .sdf (SQL Compact data file) or .mdf (SQL Server Master database file) database file that is in the App_Data folder. (Do not include the file-name extension.) Alternatively, name can specify the name of a connection string in the Web.config file. The Open(String) method can connect to a database in two ways. The Open(String) method first searches the Web application's App_Data folder for a database file that matches the name that is passed (without the file-name extension) to the method. If no matching database file is found, the method searches the application's Web.config file for a connection string that matches name.

Opens either the specified database file or the database specified using a 
named connection string from the Web.config file.
// Note that no filename extension is specified.
var db = Database.Open("SmallBakery"); // Opens SmallBakery.sdf in App_Data
// Opens a database by using a named connection string.
var db = Database.Open("SmallBakeryConnectionString");
Opens a database using the connection string. (This contrasts with Database.Open, which uses a connection string name.)
var db = Database.OpenConnectionString("Data Source=|DataDirectory|\SmallBakery.sdf");

Database.Query Method

Executes a SQL query that returns a list of rows as the result. This method returns IEnumerable object.

Database.Query(SQLstatement[, parameters])
Queries the database using SQLstatement (optionally passing parameters) and returns the results as a collection. 
foreach (var result in db.Query("SELECT * FROM PRODUCT")) {
foreach (var result = db.Query("SELECT * FROM PRODUCT WHERE Price > @0", 20)) 
{ @result.Name

Database.Execute Method

Executes SQLstatement (with optional parameters) such as INSERT, DELETE, or UPDATE and returns a count of affected records.

Database.Execute(SQLstatement [, parameters]

Database.GetLastInsertId() Method

This method returns the identity column from the most recently inserted row.

db.Execute("INSERT INTO Data (Name) VALUES ('Smith')"); 
var id = db.GetLastInsertId();

Database.QuerySingle Method

Executes SQLstatement (with optional parameters) and returns a single record.

Database.QuerySingle(SQLstatement [, parameters])
var product = db.QuerySingle("SELECT * FROM Product WHERE Id = 1"); 
var product = db.QuerySingle("SELECT * FROM Product WHERE Id = @0", 1);

Href Property

Creates a browser-compatible URL from a local file path, with optional additional path parts.

Href(path [, param1 [, param2]])

IsPost Property

Returns true if the request is a POST. (Initial requests are usually a GET.)


Gets additional data from the URL (for example, /MyPage/ExtraData).

var pathInfo = UrlData[0];

Following is the step by step example:

Open Webmatrix.

You can download the webmatrix from  Here

Click on Site from Template option

Specify a meaningful name and click on Ok.

Create a file on selecting files and click on Create New File.

List Details: Following is the code for selecting data.

Create another file to add movie data.

another file to update data.

You can specify the default document as below.