ASP.Net MVC 6 New Features

Overview of Model View Controller (MVC)

Model View Controller (MVC) is a framework for building web applications using a specified design.

  • M stands for model.
  • V stands for view.
  • C stands for controller.
Model

Model represents the part of the application that handles the logic for application data. Mainly the Model object retrieves and stores data in a database.

Views

Views are that part of an application that handles the display of data. Views are created from model data.

Controller

The Controller is the part of the application that handles user interaction. It is the bridge between the Model and the View. Typically the Controller reads the data from the View and controls the user input and sends input data to the model.

Let's see what is new in ASP.NET MVC 6

Full Framework vs. Cloud-optimized Framework

In MVC 6 Microsoft removed the dependency of System.Web.Dll from MVC6 because it's so expensive that typically it consume 30k of memory per request and response, whereas now MVC 6 only requires 2k of memory per request and the response is a very small memory consumtion.

The advantage of using the cloud-optimized framework is that we can include a copy of the mono CLR with your website. For the sake of one website we do not need to upgrade the .NET version on the entire machine. A different version of the CLR for a different website running side by side.

MVC 6 is a part of ASP.NET 5 that has been designed for cloud-optimized applications. The runtime automatically picks the correct version of the library when our MVC application is deployed to the cloud.

 

The Core CLR is also supposed to be tuned with a high resource-efficient optimization.

Microsoft has made many MVC, Web API, WebPage and SignalLr peices we call MVC 6.

Most of the problems are solved using the Roslyn Compiler. In ASP.NET vNext uses the Roslyn Compiler. Using the Roslyn Compiler we do not need to compile the application, it automatically compiles the application code. You will edit a code file and can then see the changes by refreshing the browser without stopping or rebuilding the project.

Run on hosts other than IIS

Where we use MVC5 we can host it on an IIS server and we can also run it on top of an ASP. Net Pipeline, on the other hand MVC 6 has a feature that makes it better and that feature is itself hosted on an IIS server and a self-user pipeline.

Environment based configuration system

The configuration system provides an environment to easily deploy the application on the cloud. Our application works just like a configuration provider. It helps to retrieve the value from the various configuration sources like XML file.

MVC6 includes a new environment-based configuration system. Unlike something else it depends on just the Web.Config file in the previous version.

Dependency injection

Using the IServiceProvider interface we can easily add our own dependency injection container. We can replace the default implementation with our own container.

Supports OWIN

We have complete control over the composable pipeline in MVC 6 applications. MVC 6 supports the OWIN abstraction.

Important components of an MVC6 application

The is a new file type in MVC6 as in the following:



The preceding files are new in MVC 6. Let's see what each of these files contain.

Config.Json

This file contains the application configuration in various places. We can define our application configuration, not just this file. There is no need to be concerned about how to connect to various sources to get the confutation value.

In the following code we add a connection string in the Config.json file.

  1. {  
  2.     "Data": {  
  3.         "DefaultConnection": {  
  4.             "ConnectionString""Server=server_name;Database=database_name;Trusted_Connection=True;MultipleActiveResultSets=true"  
  5.         }  
  6.     },  

Project.json

This file contains the build information as well as project dependencies. It can contain the commands used by the application.

  1. {  
  2.       
  3.     "webroot""wwwroot",  
  4.     "version""1.0.0-*",  
  5.     "dependencies": {  
  6.         "Microsoft.AspNet.Mvc""6.0.0-beta1",  
  7.         "Microsoft.AspNet.Server.IIS""1.0.0-alpha4",  
  8.     },  
  9.     "commands": {  
  10.         /* Change the port number when you are self hosting this application */  
  11.         "web""Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000",  
  12.   
  13.     },  
  14.     "frameworks": {  
  15.         "aspnet50": { },  
  16.         "aspnetcore50": { }  
  17.     },  
  18.   
  19. } }  

Startup.cs

The Application Builder is used as a parameter when the configure method is used when the Startup class is used by the host.

Global.json

Define the location for the project reference so that the projects can reference each other.

Defining the Request Pipeline

If we look at the Startup.cs it contains a startup method. 

  1. public Startup(IHostingEnvironment env)  
  2. {  
  3.             // Setup configuration sources.  
  4.             var Configuration = new Configuration();  
  5.             Configuration.AddJsonFile("config.json");  
  6.              Configuration  .AddEnvironmentVariables();  
  7.         }  

Create a Controller

If we look at the controller it is similar to the controller in a MVC 5 application. 

  1. public class HomeController : Controller  
  2. {  
  3.         public IActionResult Index()  
  4.         {  
  5.             return View();  
  6.         }  

The namespace for MVC6 is Microsoft.ASPNET.MVC unlike the System.Web.MVC in the previous version. That is a big difference. In MVC 6 the application does not need to be derived from the controller class. In our controller there are many defaults functionalities. Most of the time we might need to derive from the controller class, but if we do not need access to all of the functionally provided by the controller class, we can define our controller class. 

  1. public class HomeController : Controller  
  2. {  
  3.         public ActionResult Index()  
  4.         {  
  5.             return View();  
  6.         }  
  7.         public string atul()  
  8.         {  
  9.             return "hello i am mvc6";  
  10.   
  11.         }  

 If you execute the preceding method we may see the following page.


Summary

By this article we have learned about the new features of MVC 6. We have now become familiar with MVC 6.

Up Next
    Ebook Download
    View all
    Learn
    View all