What is Razor View Engine in MVC?
Razor View Engine:
The Razor view engine employs a syntax that on one hand is familiar to nearly all ASP.NET developers. On the other hand, the ASPX markup was devised as a way to support server controls, and it has severe limitations when used mostly with code blocks. In ASP.NET MVC, the Razor view engine provides an alternate markup language to define the structure of view templates. According to Razor, a view template is an HTML page with a few placeholders and code snippets.
Overall, the readability of the view template is greatly improved, and by combining Razor code snippets with HTML helpers, you can arrange views to make them easier to read and maintain. The Razor view engine supports a bunch of interesting properties that are related to the locations of the view templates.
List of location formats for Razor View Engine:
AreaMasterLocationFormats: Locations where master views are searched in the case of area-based applications.
AreaPartialViewLocationFormats: Locations where partial views are searched in the case of area-based applications.
AreaViewLocationFormats: Locations where views are searched in the case of area-based applications.
MasterLocationFormats: Locations where master views are searched.
PartialViewLocationFormats: Locations where partial views are searched.
ViewLocationFormats: Locations where views are searched
FileExtensions: List of extensions supported for views.
Razor View Object:
When the Razor view engine is used, the resulting view object is an instance of the
class defined in the
System.Web.Mvc assembly. This class incorporates the logic to parse markup and render HTML.
Methods of a Razor View Object:
Ajax: It gets an instance of the AjaxHelper class used to reference Ajax HTML helpers around the template.
Culture: It gets and sets the ID of the culture associated with the current request. This setting influences culture-dependent aspects of a page, such as date, number, and currency formatting. The culture is expressed in an xx-yy format, where xx indicates the language and yy the culture.
Href: It converts paths that you create in server code (which can include the ~ operator) to paths that the browser understands.
Html: It gets an instance of the HtmlHelper class used to reference HTML helpers in the template.
Context: It gets the central repository to gain access to various ASP.NET intrinsic objects: Request, Response, Server, User, and the like.
IsAjax: It returns true if the current request was initiated by the browser’s Ajax object.
IsPost: It returns true if the current request was placed through an HTTP POST verb.
Layout: It gets and sets the path to the file containing the master view template.
Model: It gets a reference to the view model object (if any) containing data for the view. This property is of type dynamic.
UICulture: It gets and sets the ID of the user-interface culture associated with the current request. This setting determines which resources are loaded in multilingual applications. The culture is expressed in an xx-yy format, where xx indicates the language and yy the culture.
ViewBag: It gets a reference to the ViewBag dictionary that might contain data the controller needs to pass to the view object.
ViewData: It gets a reference to the ViewData dictionary that might contain data the controller needs to pass to the view object.