MVC frequently asked questions

Following are some of the MVC frequently asked questions .These questions are commonly asked in interviews and will also help you to get better understanding of the framework.

What is ASP.NET MVC?                                                                                                                                     ASP.NET MVC is a web development framework used for creating web applications on the ASP.NET platform.It is an alternative to web forms for creating web applications.It is based on the Model View Controller architectural pattern which used for creating loosely coupled applications.The three main components in a MVC application are :

  • Model represents the business entities
  • View used to render the UI
  • Controller receives the end user request,performs operations on the model as required and renders the view.

What is the flow of the Request in MVC application?  

At a high level we can depict the flow of the request with the following diagram:                                    MVC Request flow

UrlRouting module receives the request and it passes the request to the matching controller.Controller queries or updates the model and returns the view to handle the request.

What are the main differences between ASP.NET WebForms  and ASP.NET MVC?

There is no proper separation of concerns,the application logic resides in the code behind of the webform ,so the .aspx page and its code behind are tightly coupled.This makes it difficult to make the changes in one without effecting the other.In the case of ASP.NET MVC there is a separation of concerns ,so the Model,View and Controller are loosely coupled.This means that we can easily make changes in one component without effecting the other components.
There is viewstateNo Viewstate
Limited control over the generated HTMLprovides complete control over the generated HTML.

What is Routing in MVC?    

In WebForms the URL’s are mapped to the physical files on the file system.But in the case of ASP.NET MVC URL’s are not mapped to the physical files but are mapped to the controllers and action methods.This mapping is doned by the routing engine.To map the URL to the controllers and action methods ,the routing engine defines the routes.Based on the matching route the request is handled by the appropriate controller.

We can depict the role of the routing engine with the following diagram:

MVC Routing Engine


What are Action Methods?
Action methods are defined by the controllers ,urls are mapped to the action methods.
The request which is received by our mvc application is ultimately handled by an action method.Action method generates the response in the  form of ActionResult.The action method to execute is determined according to the routing rules defined by our application.

What is ActionResult?
ActionResult is a class which represents the result of an action method.Action methods returns an object of a type which derives from this ActionResult class.Since ActionResult is an abstract class so it provides few derived classes whose object the action method can create to return the response.Also there are few methods available to the controller class to create ActionResult subclass,so we don’t need to explicitly create an object of the ActionResult  and can just call the method.
Some of the classes deriving from the ActionResult are:

Action ResultHelper MethodDescription
ViewResultViewRenders a view
PartialViewResultPartialViewRenders a partial view, which is a view which can be used inside another view.
RedirectResultRedirectRedirects to different action method as specified in the URL.
RedirectToRouteResult RedirectToRouteRedirects to another action method.
ContentResultContentReturns the user supplied content type.
JsonResultJsonReturns a JSON object.

What are HTML helpers?

HTML helpers are methods which returns HTML strings.There are few inbuilt HTML helpers which we can use.If the inbuilt helpers are not meeting our needs ,we can also create our custom HTML helpers.They are similar to the webforms controls as both the webforms controls and the MVC HTML helpers returns HTML.
But HTML helpers are lightweight compared to the webforms controls.
Following are some of the commonly used HTML helper methods for rendering the HTML form elements

  • BeginForm()
  • EndForm()
  • TextArea()
  • TextBox()
  • CheckBox()
  • RadioButton()
  • DropDownList()
  • Hidden()
  • Password()

What are strongly typed Helpers?
In the case of normal helper methods we need to provide the string values to the helper methods.As these are string literals so there is no compile time checking and also intellisense support is available.

In contrast strongly typed helper methods takes lambda expressions so they provide
intellisense support and also are type checked at compile time.

Following are the normal and strongly typed helper methods which generates the same HTML
Html.TextBoxFor(model => model.Name)

What are Action Filters?
Sometimes we want to execute some logic either before the execution of the action method or after the execution of the action method.We can use Action Filter for such kind of scenario.ActionFilters defines logic which is executed before or after the execution of the action method.ActionFilters are attributes which we can apply to the action methods.

Where are the routes for the application defined?
When the application starts ,the RegisterRoutes method is called from the application_start method of glabal.asax.In the RegisterRoutes method routes are added to the route table using the MapRoute method.




The RouteConfig.cs contains the RegisterRoutes method which is defined as

The above method is called from the Application_Start method of the Global.asax as:

Routing in MVC

 Which assembly contains the ASP.NET MVC classes and interfaces?

The main functionality of ASP.NET MVC is defined in the System.Web.Mvc assembly.It contains the classes and interfaces which supports the MVC functionality. System.Web.Mvc is the namespace which contains the classes used by the MVC application.

What is a View Engine?

View Engines are responsible for generating the HTML from the views.Views contains HTML and source code in some programming language such as C#. View Engine generates HTML from the view which is returned to the browser and rendered.Two main View Engines are WebForms and Razor ,each has its own syntax.

What are ViewData, VieBag and TempData ?

ViewData, VieBag and TempData are used for passing data from the controller to the view. ViewBag and ViewData are used to pass the data from the controller to the view while TempData can also pass the data from one controller to another controller or one action method to another action method.TempData uses session internally.

What are the new features of MVC5?

Some of the features included in MVC5 are

  • Scaffolding
  • ASP.NET Identity
  • One ASP.NET
  • Bootstrap
  • Attribute Routing
  • Filter Overrides

What is Bundling and Minification?

Bundling and Minification is used for improving the performance of the application.Bundling reduces the number of HTTP requests made to the server by combining several files into a single bundle.Minification reduces the size of the individual files by removing unnecessary characters.