An overview of MVC 6
ASP.NET 5 is the next version of ASP.NET, it was previously called ASP.NET vNext.
MVC 6 is part of ASP.NET 5. As we can expect with any new version of a framework many changes are introduced but lot more concepts and changes are introduced in MVC 6.This is because the underlying ASP.NET framework has been rebuild from the ground up so it reflects in the MVC framework as well.
We have used the following frameworks for different scenarios.
Different Frameworks for different scenarios
Useful in scenario
Web applications which have separation of concerns and are testable.
Web services that target different types of devices
It's Lightweight framework for building smaller web applications
Some Disadvantages of MVC 5
These frameworks serves different purposes perfectly. When working with these frameworks we realize that conceptually they are very similar.
MVC and WebAPI have lots of concepts in common like
Controllers Actions Filters Model binding
But still each uses entirely different types in different namespaces and assemblies. Below are a few examples:
WebAPI and MVC both have controllers and the purpose of controllers in both of these frameworks is to handle the incoming HTTPRequest. But the WebAPI controllers inherit from the ApiController class where as the MVC controllers inherit from the Controller class.
WebPages and MVC both are used to build UI applications and both use HTMLHelpers for creating UI functionality. But the implemention of the HTMLHelpers is entirely different.
For WebPages HTML helpers are defined in System.Web.WebPages.Html namespace in System.Web.WebPages assembly. For MVC HTML helpers are defined in System.Web.Mvc namespace in the System.Web.Mvc assembly.
The different implementations of these frameworks has the following disadvantages.
We have to learn about the different types in different namespaces even though there purpose is same.For example the implementation of the routing infrastructure in Web API is entirely different from MVC and we as developers have to struggle to learn two different ways to do the same thing.
There is a duplication of functionality in the framework classes as the types for the frameworks are implemented in different assemblies .The consequence is there are separate updates and new releases of the above frameworks even for the same functionality like routing or model binding.
They seem to fit together well, sharing lot of common concepts rather then being segregated into different frameworks.
This is what MVC 6 is. It merges the three frameworks into a single framework. MVC 6 consists of all these frameworks. Though it is called MVC 6 but it consists of all the three frameworks. So it consists of the next versions of MVC5 , WebAPI 2 and WebPages2.
We can use our existing knowledge of these frameworks in MVC 6 as well but as the underlying core ASP.NET framework has changed so there are many new concepts that we may need to use in MVC 6.
One of the big changes in ASP.NET 5 is that it doesn’t run on top of the old ASP.NET pipeline that consists of HTTPHandlers and HTTPModules .It runs on top of a new composable pipeline in which we can add middleware components.
New features in MVC 6
Can be run other hosts then IIS
While MVC5 can be hosted in IIS and runs on top of asp.net pipeline ,MVC 6 can be self hosted and uses flexible pipeline in which we have complete control over the components that are part of the pipeline.
Since MVC 6 is part of the ASP.NET 5 ,which 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.
Environment based configuration system
MVC 6 includes new environment based configuration system,unlike depending on just the web.config file as in the previous versions.Deploying MVC 5 applications to the cloud requires many configuration changes.MVC 6 applications can be very easily deployed to the cloud because of the environment based configuration system.Our application works with the configuration providers which retreives the value from the different configuration sources like XML file.
In MVC 6 dependency injection is supported across all the technologies , WebAPI,MVC and WebPages.A default dependency injection container is provided out of the box which provides minimal functionality and is useful when we require only limited functionality.We can very easily add our own dependency injection container by implementing the IServiceProvider interface.This is the interface that is implemented by the deafult container as well ,so we can replace the default implementation with our own conatiner.
MVC 6 supports the OWIN abstraction.So MVC 6 applications consists of composable pipeline in which we have complete control over the various components in the pipeline.
Important components of an MVC 6 Application
New file types in MVC 6
When we create a new ASP.NET 5 application few files are added to the solution explorer.
As these are new files in MVC 6 application let’s have a look into what each of these files contains.
Config.json This file contains the application configuration information. We can define our application configuration in different places not just this file. Using the configuration value providers the correct configuration values are picked. So our application is not concerned how to connect to different sources to get the configuration values.
Below we have added a connection string in the config.json file.
Hide Copy Code
Project.json This file contains the project dependencies as well as the build information. This can also contain the commands used by the application.
Hide Copy Code
Startup.cs By default the host looks for a class called Startup with a Configure method that takes IApplicationBuilder as a parameter.
Global.json defines the location for project references so that projects can reference each other.
NuGet Packages If we right click the ASP.NET reference node ,there is no option to add the assembly reference.It is because the unit of reference in MVC 6 application is nuget package and not assembly unlike the previous version.
Other nice feature of nuget references is that we don't need to specify all the dependent nuget packages instead they are referenced by default.In the solution explorer we can see all the dependencies for the packages nad we do not need to specify all these packages ,rather we just specify the main nuget package and the rest are added automatically by the framework.
Defining the Request Pipeline
If we look in the startup.cs file it contains a Configure method
Hide Copy Code
IApplicationBuilder is the parameter which is passed by the host when the application starts. The above code creates an object of configuration class.We are adding the conjig.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 our application don'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.
In Configure method we need to add the services used by our application to the services container. UseServices is an extension method which we use to add the services we require in our application.We need to pass a parameter of type servicecollection to which we add all the required services for our application.
For example to add the service for EntityFramework we use AddEntityFramework() extension method defined for the IServiceCollection interface.
Hide Copy Code
In MVC 6 we need to explicitly add the middleware components to the request pipeline unlike the previous versions which have a well defined pipeline.We add the components using the UseXXX extension methods.If we want to add the middleware for MVC then we add it using the UseMvc extension method.
Hide Copy Code
The routing component is defined in the Microsoft.ASPNET.Routing class which is common to Web API and MVC.This is unlike previous versions in which routing was included in System.Web.Routing namespace.
Like rest of the components we setup in the request pipeline ,we add routes as a middleware component.In the above route we have made the id as optional atribute by adding the ? after id.
Creating a controller
If we look at the controller it is similar to the controller in MVC 5 application.
Hide Copy Code
Though there are few differences .
Namesapce for MVC 6 is Microsoft.AspNet.Mvc unlike System.Web.Mvc in the previous versions.
One other big difference between the controller in MVC 5 application and MVC6 is that a controller in MVC 6 application does not need to derive from the Controller class.Though most of the time we may need to derive from the controller class as it provides lots of default functionality available to our controller.But if we don’t need access to all the functionality provided by the controller class then we can define our controller class as a normal class or POCO.
So if we change our HomeController to a normal class it is perfectly valid in MVC 6
Hide Copy Code
If we execute the above action method we may see the below page.Our normal c# class ,without being inherited from the Controller class , is working as a controller .
The controller class is defined in the Microsoft.AspNet.Mvc namespace unlike System.Web.Mvc in the previous versions.