Razor Way | ASP.Net Tip

Razor is a view engine of ASP.NET  MVC .It’s primary objective is to  implement a syntax which is compact ,easy and more fluid. It’s vey helpful in maintaining the flow of  code and markup together with very less hinderance from the control character which programmar has to enter every time he shifts betwenn  code and markup.So obviously life is much easier  the Razor Way.

In ASP.NET MVC  we can choose the syntax we want to use when we create a new view template file.  It will allow us to easily select any of of the available view engines we have installed on our machine.File extensions  will be either  .cshtml  or  .vbhtml  depending on what language
we are using C# or VB.

Here is a brief view of  how things actually work in this new view engine using a simple example.

This is a normal code in a  .aspx file.As we can see there is a mixup of  markup along with code .Adding more to the mess are the control characters  “”.

  • ($)

But what we actually want is a bit less complicated.In the above piece of code  if we remove the unwanted control characters,we are left with this simplified version of the code (written below) . But obviously this  will not work as it’s missing some thing which will tell parser difference between markup and code.

    foreach(var x in Category.Items) {

  • x.Name ($x.Price)
  • }

Now comes the much simpler  Razor syntax .If we write the above lines using Razor syntax here is the code.

.

In this syntax  ‘@’  is the real hero of the game . ‘@’ indicates the path change from markup to code to the parser.It’s a single character replacement of  it’s 3 character equivalent in .aspx  format  ‘’ which indicates end of the code block in .aspx. In razor syntax the parser is intelligent enough to determine the end of the code block after it encounters the begining  by ‘@’ as it  uses the syntax of the underlying language to determine when the code block is finished.

So if we go by numerical figures ‘@’ alone replaces 5 key strokes which is a great achievment on which Razor can be proud of itself.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!