Click here to Skip to main content
13,148,434 members (58,815 online)
Click here to Skip to main content
Add your own
alternative version

Stats

84.4K views
29 bookmarked
Posted 17 Nov 2014

MVC 6 overview

, 17 Nov 2014
Rate this:
Please Sign up or sign in to vote.
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.

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.

 

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.

{
    "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.

{
    "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.

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

[__em__]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.

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.

[__em__]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.

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

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 .

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)

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
Questionnice Pin
Vinod Jangle28-Apr-17 12:40
memberVinod Jangle28-Apr-17 12:40 
Questioni think mvc6 is a bit more Pin
PGT2-Nov-16 14:13
memberPGT2-Nov-16 14:13 
QuestionHow dlls are created in asp.net 5 Pin
mahonealex28-Sep-16 22:01
membermahonealex28-Sep-16 22:01 
GeneralMy vote of 5 Pin
Dennis Fazekas18-May-16 1:55
memberDennis Fazekas18-May-16 1:55 
QuestionNicely written Pin
Pradeep Shet12-May-16 23:09
memberPradeep 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
memberneel bhatt9-Sep-15 22:19 
QuestionAuthorization not working in ASP.NET MVC 6 Pin
marcosph5-Jul-15 7:38
membermarcosph5-Jul-15 7:38 
AnswerRe: Authorization not working in ASP.NET MVC 6 Pin
PGT2-Nov-16 14:15
memberPGT2-Nov-16 14:15 
QuestionNo more web forms are part of ASP.net 5 OR MVC 6 Pin
Jahangir Shahzad22-Jun-15 21:19
memberJahangir Shahzad22-Jun-15 21:19 
QuestionGood job ..But with little mistake Pin
Member 1133912428-Dec-14 1:54
memberMember 1133912428-Dec-14 1:54 
GeneralMy vote of 5 Pin
Thomas Maierhofer (Tom)24-Nov-14 9:10
memberThomas Maierhofer (Tom)24-Nov-14 9:10 
QuestionThank you but... Pin
vasilega_7418-Nov-14 23:16
membervasilega_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
memberashish__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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170924.2 | Last Updated 18 Nov 2014
Article Copyright 2014 by ashish__shukla
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid