An overview of new features of MVC 6 for MVC Software companies
MVC, Web API and Web pages frameworks is merged into one framework called MVC 6. This is also open source framework and cross-platform compatible. This is supported on mono, Mac and Linux. MVC 6 has gained very high popularity among MVC software development companies in India in very short time. The architecture of the newly developed framework, MVC 6 is as below.
New features in MVC 6
- System.Web.Dll dependency removed
Removed the dependency of System.Web.Dll from MVC6 because it consumes 30k of memory per request and response, whereas MVC 6 only requires 2k of memory per request and the response consumes very small memory.
Single based Controller for MVC and Web API
In MVC 6, there is only one Controller base class for both MVC and Web API Controller that is Microsoft.AspNet.Mvc.Controller. Controller class System.Web.MVC.Controller for MVC and System.Web.Http.ApiController for web API are merged into one class.
MVC 6 controllers return an IActionResult. When used as an MVC controller, the IActionResult is a view. When used as a Web API controller, the IActionResult is data such as a list of students etc. The same controller can have actions that return both views and data.
In MVC 6, both MVC controllers and Web API controllers use the same routes. MVC software developer can use either convention-based routes or attribute routes and they apply to all controllers in a project.
Supports other hosts in addition to IIS
One can host MVC5 on an IIS server and can be run on top of an ASP.NET Pipeline. On the other hand, MVC 6 is self-hosted and uses flexible pipeline that one can control completely over the components that are part of the pipeline.
- Environment based configuration system
The configuration system provides an environment to easily deploy the application on the cloud. An application works just like a configuration provider. It helps in retrieving the value from the various configuration sources like XML file. So nowMVC software programmers do not need to depend on just Web.config.
New Roslyn Compiler
Using the Roslyn Compiler, it automatically compiles the application code. MVC Programmer can edit a code file and can see the changes by refreshing the browser without stopping or rebuilding the project.
MVC 6 is a part of ASP.NET 5 that is designed for cloud-optimized applications. It automatically picks the correct version of the library runtime when MVC is deployed to the cloud and session state and caching gets adjusted depending upon hosting environment.
Using the IServiceProvider interface,ASP.NET software programmer can easily add its own dependency injection container. One can replace the default implementation with his own container.
We have complete control over the composable pipeline in MVC 6 applications. MVC 6 supports the OWIN abstraction.
Everything is packaged with NuGet
The unit of reference in MVC 6 application is nuget package and not assembly unlike the previous version. Programmer just needs to add main nuget package and all the dependent nuget packages are added automatically by the framework.
One can upload dependencies to the bin directory without affecting other apps on same server.
New JSON based project structure
MVC 6 contains global.json,config.json,project.json for different-different Configuration.
Important components of an MVC6 application
It contains solution-level settings and project-to-project references.
Source code for our web application.
This file contains the application configuration in various places. MVC Programmer can define its application configuration, not just this file. Using the configuration value providers the correct configuration values are picked. There is no need to be concerned about how to connect to various sources to get the confutation value.
Following code snippet explains how to add a connection string in the Config.json file.
Project.json file provides intellisense for the available packages when developer adds or edits dependencies. All packages that one has added from this file are automatically pulled from NuGet and when one removes packages it automatically gets removed from project reference.
It is an entry point for the application. This file is used to define the environment for the application, e.g. Using MVC, Entity Framework, etc. By default the host looks for a Startup class with a Configure method that takes IApplicationBuilder as a parameter.
Defining the Request Pipeline
the Startup.cs contains a startup method.
The above code snippet creates an object of configuration class. We are adding the config.json and Environment variables as the source of configuration values. Also we can add other configuration sources that our application requires.
Our application can fetch configuration values from different configuration sources as there are configuration value providers for different sources .So application does n't need to be concerned about the different sources of configuration. This is one reason that MVC 6 applications can be easily moved to cloud.
Code snippet to add the MVC framework in the pipeline
The code above is same as the old MVC for defining routes, except that the UseMvc method is used to setup the routes.
The AddMvc() basically adds the dependencies needed for MVC 6.
Understanding Controller in MVC 6
In MVC 6 the application does not need to be derived from the controller class. In controller there are many default functionalities. Most of the time one needs to derive from the controller class, but if one does not need access to all of the functionally provided by the controller class, one can define its own controller class.
Understanding Views in MVC 6
In MVC 6 view, developers have been provided with new attributes for Model binding. HTML Helper methods are used for generating UI elements like : @Html.EditorFor and Model binding are removed.
MVC 6 New Attribute
- Asp-controller: Provides Controller Name
- Asp-action : Provides Action Name of Controller class.
- Asp-for : This attribute is used for Model Binding
- Asp-validation-for : This attribute is used for Model validation
- Asp-validation-summary : This attribute is used to display the validation summary.