MVC

Advantages of Razor View Engine


The Razor View Engine is an advanced view engine, available with MVC 3.0 and later versions. Razor uses the “@” character instead of “<% %>” as used by the ASPX View Engine. Razor does not require the code block to be closed, the Razor View Engine parsed itself and it is able to decide during run time that it is a presentation element (content) and that it is a code element. The Razor View Engine is compatible with a unit testing framework. The Razor template does not require the controller or webserver to host it, so views written in Razor are fully testable. The file extension of a Razor view is cshtml (for C#) and vbhtml (for VB.NET). By default all text from the @ expression is HTML encoded. Razor is not a new language. It is easy to learn. The main advantage of Razor, is that there is less transition between HTML and code because Razor provides an optimized syntax to generate HTML using a code focused templating approach.

Advantages of Razor View Engine

  • Easy to Learn: Razor is easy to learn. We can also use our existing HTML skills.
  • It is Compact, Expressive, and Fluid. Razor helps us to minimize the coding and provide us a fast and fluid coding work flow.
  • The parser (available with Razor) is smart enough. It is also able to decide at run time what is a code element and what is a content element.For example, in the following code the @ character is also part of an email address, but Razor is smart enough to identify which is code and which is static content.
           Please contact to abc@gmail.com to more information
           Current Date time : @DateTime.Now
    
  • Razor is not a new language but it is markup so that we can also use Razor with any language like C# and VB.
  • Razor also supports the concept of layout pages (the same as Master Pages in ASPX View Engine), that allows us to define a common site template, in other words a common look and feel across all the pages within a web site/application.
  • Razor does not require any special tool to write markup. We can also write our markup code with any old plain text editor like Notepad.
  • The Razor View Engine is designed such that it also supports unit test views without requiring a controller and web server. This can be hosted in any unit project. There is no special application domain required.
  • ASP.NET MVC has HTML helpers that are methods that can be invoked within a code block. All existing HTML extension methods can be used with a Razor View Engine without any code changes.
  • The code looks clean.
  • Powerful built-in validation of markup that helps us to avoid unwanted runtime exceptions due to errors in the view.
  • The Razor View Engine has the section concept that is equivalent to content placeholders in the ASPX View Engine and that can be optional.
  • The @model directive provides a cleaner and more concise way to define a strongly typed model.

One of the disadvantages of Razor is, it is not supported by visual editors like Dream Viewer.

Advertisements

Difference Between Razor View Engine and ASPX View Engine


View Engine is responsible for rendering the view into html form to the browser. By default, Asp.net MVC support Web Form (ASPX) and Razor View Engine. There are many third party view engines (like Spark, Nhaml etc.) that are also available for Asp.net MVC. Now, Asp.net MVC is open source and can work with other third party view engines like Spark, Nhaml. In this article, I would like to expose the difference between Razor & Web Form View Engine.

Razor View Engine VS Web Form (ASPX) View Engine

Razor View Engine Web Form View Engine
Razor Engine is an advanced view engine that was introduced with MVC3. This is not a new language but it is a new markup syntax. Web Form Engine is the default view engine for the Asp.net MVC that is included with Asp.net MVC from the beginning.
The namespace for Razor Engine is System.Web.Razor. The namespace for Webform Engine is System.Web.Mvc.WebFormViewEngine.
The file extensions used with Razor Engine are different from Web Form Engine. It has .cshtml (Razor with C#) or .vbhtml (Razor with VB) extension for views, partial views, editor templates and for layout pages. The file extensions used with Web Form Engine are also like Asp.net Web Forms. It has .aspx extension for views, .ascx extension for partial views & editor templates and .master extension for layout/master pages.
Razor has new and advance syntax that are compact, expressive and reduces typing. Web Form Engine has the same syntax like Asp.net Web Forms uses for .aspx pages.
Razor syntax are easy to learn and much clean than Web Form syntax. Razor uses @ symbol to make the code like as:

@Html.ActionLink("SignUp", "SignUp")
Web Form syntax are borrowed from Asp.net Web Forms syntax that are mixed with html and sometimes make a view messy. Webform uses <% and %> delimiters to make the code like as:

<%: Html.ActionLink("SignUp", "SignUp") %> 
By default, Razor Engine prevents XSS attacks(Cross-Site Scripting Attacks) means it encodes the script or html tags like <,> before rendering to view. Web Form Engine does not prevent XSS attacks means any script saved in the database will be fired while rendering the page
Razor Engine is little bit slow as compared to Webform Engine. Web Form Engine is faster than Razor Engine.
Razor Engine, doesn’t support design mode in visual studio means you cannot see your page look and feel. Web Form engine support design mode in visual studio means you can see your page look and feel without running the application.
Razor Engine support TDD (Test Driven Development) since it is not depend on System.Web.UI.Page class. Web Form Engine doesn’t support TDD (Test Driven Development) since it depend on System.Web.UI.Page class which makes the testing complex.

I hope you will enjoy the tips while programming with Asp.net MVC.

MVC Framework – ASP.NET Forms


MVC and ASP.NET Web Forms are inter-related but different models of development depending on the requirement of the application and other factors. At a high level, you can consider that MVC is a more advanced and sophisticated web application framework designed with separation of concerns and testability in mind. Both the frameworks have their advantages and disadvantages depending on specific requirements. This concept can be visualized using the following diagram:

MVC and ASP.NET Diagram

mvc_and_asp_net_stack (more…)

MVC Framework – Architecture


In the last chapter, we learn Introduction Of MVC. Now let us have a look at how the execution of an MVC application takes place when certain request comes from the client. The diagram below shows the flow:

MVC Flow Diagram

mvc_flow.jpg (more…)

MVC Framework – Introduction


What is MVC?

The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application. MVC is one of the most frequently used industry-standard web development framework to create scalable and extensible projects.

MVC Components

Model: The Model component corresponds to all the data related logic that the user works with. This can represent either the data that is being transferred between the View and Controller components or any other business logic related data. For example, a Customer object will retrieve the customer information from the database, manipulate it and update it data back to the database or use it to render data. (more…)

MVC Interview Questions & Answers


Dear readers, these ASP.NET MVC Interview Questions have been designed specially to get you acquainted with the nature of questions you may encounter during your interview for the subject of ASP.NET MVC. As per my experience good interviewers hardly plan to ask any particular question during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer.

(more…)