Click here to Skip to main content
15,868,128 members
Articles / Web Development / ASP.NET

MVC 6 overview

Rate me:
Please Sign up or sign in to vote.
4.71/5 (30 votes)
17 Nov 2014CPOL10 min read 127.5K   33   15
An overview of MVC 6

Introduction

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                                                                    
Framework          Useful in scenario                                                                                        
MVC                    Web applications which have separation of concerns and are testable.
WEB API  Web services that target different types of devices
WEB PAGES   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.

Image 1

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.

MVC 6

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.

Cloud optimized

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.

Dependency injection

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.

Supports OWIN

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.

Image 2

 

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.

C#
{
    "Data": {
        "DefaultConnection": {
            "ConnectionString":  "Server=server_name;Database=database_name;Trusted_Connection=True;MultipleActiveResultSets=true"
        }
    }
}

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

C#
{
    "webroot" : "wwwroot",
    "dependencies": {
        "Microsoft.AspNet.Mvc": "6.0.0-alpha4",
        "Microsoft.AspNet.Server.IIS": "1.0.0-alpha4",
 },

    "commands": {
        /* Change the port number when you are self hosting this application */
        "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"

    },

    "frameworks": {
        "aspnet50" : { },
        "aspnetcore50" : { }
    }

}

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.

Image 3

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.

Image 4
Defining the Request Pipeline

If we look in the startup.cs file it contains a Configure method

C#
public void Configure(IApplicationBuilder app)                                                               {                                                                                                            // Setup configuration sources

            var configuration = new Configuration();

            configuration.AddJsonFile("config.json");

            configuration.AddEnvironmentVariables();

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.

C#
app.UseServices(services =>

            {

                // Add EF services to the services container

                services.AddEntityFramework()

                    .AddSqlServer();

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.

C#
app.UseMvc(routes =>

            {

                routes.MapRoute(

                    name: "default", 

                    template: "{controller}/{action}/{id?}",

                    defaults: new { controller = "Home", action = "Index" });

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.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Mvc;


namespace HelloASP.NET.Controllers
{

    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

}

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

C#
public class HomeController
    {
        public string Index()
        {
            return "hello mvc 6";
        }

    }

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 .

Image 5

The controller class is defined in the Microsoft.AspNet.Mvc namespace unlike System.Web.Mvc in the previous versions.

 

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I have a keen interest in technology and software development.I have worked in C#,ASP.NET WebForms,MVC,HTML5 and SQL Server.I try to keep myself updated and learn and implement new technologies.
Please vote if you like my article ,also I would appreciate your suggestions.For more please visit Code Compiled

Comments and Discussions

 
Questionnice Pin
User 418025428-Apr-17 12:40
User 418025428-Apr-17 12:40 
Questioni think mvc6 is a bit more Pin
PGT2-Nov-16 14:13
PGT2-Nov-16 14:13 
i'm not entirely sure but two weeks ago i dived into aspx (i know its old).
made a basic SQL table some buttons, and worked inside sql with it.
this week started mvc6 training, i created model in models folder.
and then most got automatically generated, i mean good looking pages buttons etc.
i didn't write any code to edit the database on the razor based pages.
I don't know for previous MVC versions, (not planning to learn old coding methods)
But jumping from aspx SQL pages to automated razor cshtml views..
to me it seamed a big leap forward.
Also the default app seams pretty good, includes login database (various methods) great stuff.
jipijee

QuestionHow dlls are created in asp.net 5 Pin
mahonealex28-Sep-16 22:01
mahonealex28-Sep-16 22:01 
GeneralMy vote of 5 Pin
Dennis Fazekas18-May-16 1:55
Dennis Fazekas18-May-16 1:55 
QuestionNicely written Pin
Pradeep Shet12-May-16 23:09
Pradeep Shet12-May-16 23:09 
QuestionNICE. Pin
Navratna Pawale3-Mar-16 1:51
professionalNavratna Pawale3-Mar-16 1:51 
SuggestionNice and also refer neelbhatt40.wordpress.com/2015/08/27/hello-world-with-mvc-6 Pin
neel bhatt9-Sep-15 22:19
professionalneel bhatt9-Sep-15 22:19 
QuestionAuthorization not working in ASP.NET MVC 6 Pin
marcosph5-Jul-15 7:38
marcosph5-Jul-15 7:38 
AnswerRe: Authorization not working in ASP.NET MVC 6 Pin
PGT2-Nov-16 14:15
PGT2-Nov-16 14:15 
QuestionNo more web forms are part of ASP.net 5 OR MVC 6 Pin
Jahangir Shahzad22-Jun-15 21:19
Jahangir Shahzad22-Jun-15 21:19 
QuestionGood job ..But with little mistake Pin
Member 1133912428-Dec-14 1:54
Member 1133912428-Dec-14 1:54 
GeneralMy vote of 5 Pin
Thomas Maierhofer (Tom)24-Nov-14 9:10
Thomas Maierhofer (Tom)24-Nov-14 9:10 
QuestionThank you but... Pin
vasilega_7418-Nov-14 23:16
vasilega_7418-Nov-14 23:16 
QuestionThanks Pin
Milind Shroff18-Nov-14 21:37
professionalMilind Shroff18-Nov-14 21:37 
AnswerRe: Thanks Pin
ashish__shukla19-Nov-14 7:13
ashish__shukla19-Nov-14 7:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.