Skip to content Web Wite(Razor) Project

6 May 2011

After installing MVC 3 (on-line and off-line installers here) new templates – Web Site(Razor) – appeared in File->New->Web Site dialog.

Using this template, first thing that constantly brings to attention is site structure based on Razor view engine. As visible from the picture on the right some items has gone and bunch of cshtml (or vbhtml if you’re using VB.NET) items replaced them. These are file extensions used by Razor view engine.

Razor is stated to be “one of the view engine in ASP.NET” and such a projects (sites) will accept view templates mix using multiple view-engines within a single application or site (read more in ScottGu’s blog Introducing “Razor” – a new view engine for ASP.NET)

Second thing that shows up in the solution tree is _AppStart.cshtml.  When Razor is in charge an _AppStart.cshtml page can be used instead of a Global.asax page (which is missing in so created project). If needed _AppStart.cshtml must be in the root of the site. When found there it is run by the engine when application is up and the first request for any page in the site comes.

Running the project will result in this page:

Seems like the ASP.NET 4.0 project where similar outlook is generated by a master page. But there is no page with .master extension in the project folder.

Glancing on the files, a suspected one for this result, shows up – _SiteLayout.cshtml. About.cshtml matches to About menu, so it obviously could be ignored. Default.* is usually the file used as a starting point, so let’s open it:

Voila – Layout = “~/_SiteLayout.cshtml” – even if you’ve never heard about MVC probably will understand that the site layout is generated by the quoted file. So,  such a file might be created and used as a template for the site including the specific content in. Layout pages might have any name but should always start with an underscore(_).

Here there is something not so obvious: files which names start with underscore (_) cannot be requested directly because are prohibited internally. Rather then these files act as a master page or include files – i.e. they are used for internal purposes.

Third point is not so visible, but developer will face it when try to add something new. This is the lack of Server control or CodeBehind. All in hand is the cshtml file which contents is de facto html mixed with Razor expressions (starting with @)

Here are some equivalents with ASP.NET engin

     if(IsPost)     {
          // do something
  • Server expressions

aspx web forms server side expression <%=%> and <%…%> have morphed into @. But note that Razor @ expression does not require you to close it somehow. This improves readability and simplifies the mixture of HTML markups and server side expressions.

Of course there are lots of new things and improvements which developer will face diving deeper into development process.


From → MVC, Razor

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: