ASP.NET MVC – Overview

ASP.NET 1.0 was released on January 5, 2002 as part of the .Net Framework version 1.0. At the time, it was easy to think of ASP.NET and Web Forms as the same thing. However, ASP.NET has always supported two levels of abstraction:

System.Web.UI – Web Forms layer, including server controls, ViewState, etc.

System.Web – Provides a basic web stack, including modules, handlers, HTTP stack, etc.

When ASP.NET MVC was announced in 2007, the MVC model had become one of the most popular ways to create webmasters.

In April 2009, the ASP.NET MVC source code was released under the Microsoft Public License (MS-PL). “ASP.NET MVC framework is an easy and highly testable presentation framework, integrated with existing ASP.NET capabilities.

Some of these built-in features are master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly.

In March 2012, Microsoft released part of its web stack (including ASP.NET MVC, Razor and Web API) under an open source license (Apache License 2.0). ASP.NET Web Forms was not included in this initiative.


Microsoft decided to create their own MVC framework for building web applications. The MVC framework simply builds on top of ASP.NET. When you are building a web application with ASP.NET MVC, there will be no illusions of state, there will not be such a thing as a page load and no page life cycle at all, etc.

Another design goal for ASP.NET MVC was to be extensible throughout all aspects of the framework. So when we talk about views, views have to be rendered by a particular type of view engine. The default view engine is still something that can take an ASPX file. But if you don’t like using ASPX files, you can use something else and plug in your own view engine.

There is a component inside the MVC framework that will instantiate your controllers. You might not like the way that the MVC framework instantiates your controller, you might want to handle that job yourself. So, there are lots of places in MVC where you can inject your own custom logic to handle tasks.

The whole idea behind using the Model View Controller design pattern is that you maintain a separation of concerns. Your controller is no longer encumbered with a lot of ties to the ASP.NET runtime or ties to the ASPX page, which is very hard to test. You now just have a class with regular methods on it that you can invoke in unit tests to find out if that controller is going to behave correctly.

Benefits of ASP.NET MVC

Following are the benefits of using ASP.NET MVC −

Makes it easier to manage complexity by dividing an application into the model, the view, and the controller.

Enables full control over the rendered HTML and provides a clean separation of concerns.

Direct control over HTML also means better accessibility for implementing compliance with evolving Web standards.

Facilitates adding more interactivity and responsiveness to existing apps.

Provides better support for test-driven development (TDD).

Works well for Web applications that are supported by large teams of developers and for Web designers who need a high degree of control over the application behavior.

ASP.NET MVC – Controllers

Controllers are essentially the central unit of your ASP.NET MVC application. It is the 1st recipient, which interacts with incoming HTTP Request. So, the controller decides which model will be selected, and then it takes the data from the model and passes the same to the respective view, after that view is rendered. Actually, controllers are controlling the overall flow of the application taking the input and rendering the proper output.

Controllers are C# classes inheriting from System.Web.Mvc.Controller, which is the builtin controller base class. Each public method in a controller is known as an action method, meaning you can invoke it from the Web via some URL to perform an action.

The MVC convention is to put controllers in the Controllers folder that Visual Studio created when the project was set up.


using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCControllerDemo.Controllers {
public class EmployeeController : Controller {
// GET: Employee
public ActionResult Search(string name){
var input = Server.HtmlEncode(name);
return Content(input);

If you add a parameter to an action method, then the MVC framework will look for the value that matches the parameter name. It will apply all the possible combination to find out the parameter value. It will search in the Route data, query string, etc.

Hence, if you request for /Employee/Mark”, then the MVC framework will decide that I need a parameter with “UserInput”, and then Mark will get picked from the URL and that will get automatically passed.

Server.HtmlEncode will simply convert any kind of malicious script in plain text. When the above code is compiled and executed and requests the following URL http://localhost:61465/Employee/Mark, you will get the following output.

ASP.NET MVC – Actions

ASP.NET MVC Action Methods are responsible to execute requests and generate responses to it. By default, it generates a response in the form of ActionResult. Actions typically have a one-to-one mapping with user interactions.

For example, enter a URL into the browser, click on any particular link, and submit a form, etc. Each of these user interactions causes a request to be sent to the server. In each case, the URL of the request includes information that the MVC framework uses to invoke an action method. The one restriction on action method is that they have to be instance method, so they cannot be static methods. Also there is no return value restrictions. So you can return the string, integer, etc.

Types of Action

Actions basically return different types of action results. The ActionResult class is the base for all action results. Following is the list of different kind of action results and its behavior.


Name and Behavior



Returns a string



Returns file content



Returns file content



Returns file content



Returns nothing



Returns script for execution



Returns JSON formatted data



Redirects to the specified URL



Returns 403 HTTP Status code



Redirects to different action/different controller action



Received as a response for view engine



Received as a response for view engine

ASP.NET MVC – Filters

In ASP.NET MVC, controllers define action methods that usually have a one-to-one relationship with possible user interactions, but sometimes you want to perform logic either before an action method is called or after an action method runs.

To support this, ASP.NET MVC provides filters. Filters are custom classes that provide both a declarative and programmatic means to add pre-action and post-action behavior to controller action methods.

Action Filters
An action filter is an attribute that you can apply to a controller action or an entire controller that modifies the way in which the action is executed. The ASP.NET MVC framework includes several action filters −

OutputCache − Caches the output of a controller action for a specified amount of time.

HandleError − Handles errors raised when a controller action is executed.

Authorize − Enables you to restrict access to a particular user or role.

Types of Filters

The ASP.NET MVC framework supports four different types of filters −

Authorization Filters − Implements the IAuthorizationFilter attribute.

Action Filters − Implements the IActionFilter attribute.

Result Filters − Implements the IResultFilter attribute.

Exception Filters − Implements the IExceptionFilter attribute.

Filters are executed in the order listed above. For example, authorization filters are always executed before action filters and exception filters are always executed after every other type of filter.

Authorization filters are used to implement authentication and authorization for controller actions. For example, the Authorize filter is an example of an Authorization filter.

Let’s take a look at a simple example by creating a new ASP.Net MVC project.

Apply Action Filter

An action filter can be applied to either an individual controller action or an entire controller. For example, an action filter OutputCache is applied to an action named Index() that returns the string. This filter causes the value returned by the action to be cached for 15 seconds.

To make this a working example, let’s modify the controller class by changing the action method called Index using the following code.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo.Controllers {
public class HomeController : Controller{
// GET: Home
[OutputCache(Duration = 15)]

public string Index(){
return "This is ASP.Net MVC Filters Tutorial";

Let’s add another action method, which will display the current time.

namespace MVCFiltersDemo.Controllers{
public class HomeController : Controller{
// GET: Home

[OutputCache(Duration = 15)]
public string Index(){
return "This is ASP.Net MVC Filters Tutorial";

[OutputCache(Duration = 20)]
public string GetCurrentTime(){
return DateTime.Now.ToString("T");