Posts

Removing the Web Form View Engine for better performance of Razor View Engine

Image
By default MVC is configured to resolve a view by searching the views that match the Web Form view engine's naming conventions first. After that MVC begins search for the views that match the Razor view engine's naming conventions as shown in below fig. So, If are not using ASPX views in your MVC applications then the checking four unused locations first for every return View() and Html.RenderPartial is quite wasteful and time consuming. After removing Web Form engine, Razor View Engine will be almost twice as fast with the Web Form engine. Removing the Web Form (ASPX) view engine Removing the Web Form view engine is easy in MVC. We can remove all the view engines and add only Razor view engine by using Application_Start event of Global.asax.cs file like as: protected void Application_Start() { //Remove All Engine ViewEngines.Engines.Clear(); //Add Razor Engine ViewEngines.Engines.Add(new RazorViewEngine()); ... } After removing all the view engines and reg

Asp.net MVC Request Life Cycle

Image
While programming with Asp.net MVC, you should be aware of the life of an Asp.net MVC request from birth to death. In this article, I am going to expose the Asp.net MVC Request Life cycle. There are seven main steps that happen when you make a request to an Asp.net MVC web applications. For more details refer  Detailed ASP.NET MVC Pipeline Routing Asp.net Routing is the first step in MVC request cycle. Basically it is a pattern matching system that matches the request’s URL against the registered URL patterns in the Route Table. When a matching pattern found in the Route Table, the Routing engine forwards the request to the corresponding IRouteHandler for that request. The default one calls the  MvcHandler . The routing engine returns a 404 HTTP status code against that request if the patterns is not found in the Route Table. When application starts at first time, it registers one or more patterns to the Route Table to tell the routing system what to do with any requests th

Difference between Asp.Net WebForm and Asp.Net MVC

Asp.net framework is a part of .net platform for building, deploying and running web applications. Now, we can develop a web application by using Asp.Net Web Form and Asp.Net MVC. In this article, I am going to expose the main difference between Asp.Net Web Form and Asp.Net MVC. Difference between Asp.Net MVC and Web Forms Asp.Net Web Forms Asp.Net MVC Asp.Net Web Form follow a traditional event driven development model. Asp.Net MVC is a lightweight and follow  MVC (Model, View, Controller) pattern  based development model. Asp.Net Web Form has server controls. Asp.Net MVC has html helpers. Asp.Net Web Form has state management (like as view state, session) techniques. Asp.Net MVC has no automatic state  management techniques. Asp.Net Web Form has file-based URLs means file name exist in the URLs must have its physically existence. Asp.Net MVC has route-based URLs means URLs are divided into controllers and actions and moreover it is based on  control

Understading Controllers and Actions in MVC Razor

Image
Asp.net MVC Controllers are responsible for controlling the flow of the application execution. When you make a request (means request a page) to MVC applications , a controller is responsible for returning the response to that request. A controller can have one or more actions. A controller action can return different types of action results to a particular request. Controller Basically, the controller Receives input like as form values, query strings values etc. from users via the View and perform required operations on the user's inputs with the help of Model and passing the results back to the View. Actually, controllers are classes that have methods or action results and these actions results are called by the routing system to process a particular request. Creating a Controller For creating a controller, do right-click on the Controller folder in your mvc application and select the menu option Add, Controller. After selection the Add Controller dialog is being displ

Partial View in Asp.net MVC3 Razor

Image
A partial view is like as user control in Asp.Net Web forms that is used for code re-usability. Partial views helps us to reduce code duplication. Hence partial views are reusable views like as Header and Footer views. We can use partial view to display blog comments, product category, social bookmarks buttons, a dynamic ticker, calendar etc. For understanding the different rendering ways of partial view refer the article  RenderPartial vs RenderAction vs Partial vs Action in MVC Razor Creating A Partial View A partial view has same file extension(.cshtml) as regular view. To create a partial view do right click on shared folder (\Views\Shared) in solution explorer and click on "Add New View" option and then give the name for partial view and also checked the Create a partial view option as shown in fig.   Note It is best practice to create partial view in the shared folder and partial view name is preceded by "_", but it is not mandatory. The "_

Understanding ViewModel in ASP.NET MVC

In ASP.NET MVC, ViewModel is a class that contains the fields which are represented in the strongly-typed view. It is used to pass data from controller to strongly-typed view. Key Points about ViewModel ViewModel contain fields that are represented in the view (for LabelFor,EditorFor,DisplayFor helpers) ViewModel can have specific validation rules using data annotations or IDataErrorInfo. ViewModel can have multiple entities or objects from different data models or data source. ViewModel Example Designing ViewModel public class UserLoginViewModel { [ Required ( ErrorMessage = "Please enter your username" )] [ Display ( Name = "User Name" )] [ MaxLength ( 50 )] public string UserName { get ; set ; } [ Required ( ErrorMessage = "Please enter your password" )] [ Display ( Name = "Password" )] [ MaxLength ( 50 )] public string Password { get ; set ; } } Presenting the viewmodel in the v