Click here to Skip to main content
Click here to Skip to main content
Go to top

New Features in ASP.NET MVC 4

, 17 Nov 2013
Rate this:
Please Sign up or sign in to vote.
New features in ASP.NET MVC 4

Introduction of MVC 4 New Features

MVC 4 can be installed in Visual Studio 2010 from here and MVC 4 will be default project template in Visual Studio 2012.

There are many new features that have been introduced in MCV 4 with respect to project templates and many new features have been added to the framework.

MVC 4 has been officially launched on 15-August-2012.

MVC 4 Features

MVC 4 features has been categorised in 2 ways as below:

  1. MVC Framework
  2. MVC Project Templates

I. MVC 4 Framework

1. Add Controller to other project folder

Till MVC 3, controller class can be added only in the “Controllers” folder in the project, adding controller class to other folder was not allowed but from MCV 4, we can add the controller class to any other folder, this way we can better arrange our controller classes in the project.

Example: Suppose we have a hotel module in the project and would like to have a “hotel” folder inside the controller folder and create all hotel related controllers. Follow the below steps to add new controller class:

Step 1: Create a “Hotel” folder inside the “Controllers” folder .

Step 2: Add controller as below:

Step 3: Project folder structure as below:

2. Task Support for Asynchronous Controllers

MVC 4 controller class is built on top of .NET 4.5. This enables us to write the asynchronous action method that returns an object type of Task. .NET 4 introduced an enhanced way of writing asynchronous programming that is also supported by MVC 4.

I am not providing complete details as this article will be out of scope. I will be writing another one soon.

Sample of asynchronous controller method is as follows:

public async Task<actionresult> HotelAsync()
{
   var objHotelService = new HotelService();

   return View("Hotels", await objHotelService.GetAllHotelsAsync());
}    
3. Bundling and Minification

Bundle and minification framework will reduce the number of hits internally made by HTTP request for a web page. Also, this will reduce the size of overall request with minification.

Bundling: Bundling feature starts with ASP.NET 4.5. Bundle feature will combine the multiple files into a single file. We can create JavaScript, CSS and other bundles. Bundling will improve the performance of the web request as this will have less number of files which means less HTTP requests.

Minification: Minification will just do the code optimization to the scripts and CSS files by removing the unnecessary white space, comments and shortening variable names to one characters.

Controlling Bundling and Minification: Bundling and minification is enabled or disabled by setting the value in Web.config file compilation debug attribute.

compilation debug="true"  : Disabled 
compilation debug="false"  : Enabled
              
system.web
compilation debug="true" 
system.web     

We can override the Web.config setting with the EnableOptimizations property on the BundleTable class. In MVC 4 with Visual Studio 2012, we can find “App_Start” folder in that we have “BundleConfig.cs” in this we can add EnableOptimizations property and set the value.

BundleTable.EnableOptimizations = true; //Enabled Bundling 
minification BundleTable.EnableOptimizations = false; //disabled Bundling and minification 

Example:

public static void RegisterBundles(BundleCollection bundles)
    {
        BundleTable.EnableOptimizations = true;

        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new StyleBundle
        ("~/Content/css").Include("~/Content/site.css"));

        bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                    "~/Content/themes/base/jquery.ui.core.css",
                    "~/Content/themes/base/jquery.ui.theme.css"));            
     }

Note: Either Web.config 'compilation debug="false"' or 'BundleTable.EnableOptimizations=True' has to be set otherwise bundling and minification will not work.

4. Enabling Logins from Facebook and Other Sites Using OAuth and OpenID

MVC 4 has a default template “Internet Project” that supports for OAuth and OpenID login using the DotNetOpenAuth library.

5. App_Start folder and separate classes

MVC 4 with Visual Studio 2012 as soon as we will create new MVC 4 application “App_Start” folder gets created with the classes as below:

  • RouteConfig.cs: This class for the route table collection data. We have a static function “RegisterRoutes” for the route collection data and URL pattern defined here. For those who are using Visual Studio 2010, you will find the “RegisterRoutes” function in the global.asax.
  • BundleConfig.cs: This is a new feature in MVC 4 for bundling and minification, above I had explained how this is working.
  • WebApiConfig.cs: This is a new feature in MVC 4 and this class is similar to the “RouteConfig” class, the only difference is this class will do the routing for the ASP.NET Web API (this is kind of REST service)
  • FilterConfig.cs: This is for the Action filter logic.
  • AuthConfig.cs: This is a new feature in MVC 4 and this will be used for the OAuth and OpenID authentication.
6. Display Modes

With new display modes, view will be selected depending on the request browser. In other words, we can say if desktop browser will request then desktop version of view will be selected. If mobile browser will request then mobile version of view will be selected as for other devices.

Probably I will be writing complete details in other blog.

7. Azure SDK

ASP.NET MVC 4 supports the 1.6 and newer releases of the Windows Azure SDK.

8. Database Migrations

MCV 4 included the entity framework 5. Entity framework 5 supports the data migration, this is one of the great features available.

II. MVC Project Templates

1. ASP.NET Web API

MVC 4 included new project template “Web API” for building HTTP service on top of MVC framework. This is lightweight service and can return JSON or XML data. Service will have broad reach of client including browser, mobile device, tablet or even TV set up box. ASP.NET web API is also ideal platform for building the RESTfull services.

Here are a few steps to create a web API:

Step 1: Select the ASP.NET MVC 4 Web Application project template as below:

Step 2: Now Select Web API as below:

Step 3: API controller class snap shot:

2. Mobile Project Template

MVC 4 includes a new project template “Mobile Application”. If we have to develop a web site specific for mobile or tablet device, then we can use this mobile application project template. This is based on JQuery Mobile, an open source library for building touch optimize UI.

3. Empty Project Template

Empty project template is now completely empty so that we can start from the beginning. The earlier version of empty project is renamed as “basic”.

4. Enhancements to Default Project Templates

The default project template has a new look and feel as per modern website look. Also there is an adaptive rendering technique that been implemented to that site, will look good in desktop and mobile browser without any changes.

Summary

I have tried to explain all the new features available in the ASP.NET MVC 4. Hope you have enjoyed reading the entire topic. Please provide your vote, suggestions and feedback... to encourage me to write more blogs.

License

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

Share

About the Author

Afazal MD 3104209
Technical Lead Infinite Computer
India India
10+ years of experience in designing and developing Microsoft technologies applications.
My expertise skills are ASP.Net,MVC 3/4,MVC 4 WEB API, C#, WCF, REST, UML, Design Pattern and SQL server.
 
You can reach me @ mdafazal@gmail.com

Comments and Discussions

 
Generalgood article Pinmembersachin shelake27-Apr-14 7:08 
GeneralMy vote of 4 Pinmembervmsrdy19-Nov-13 15:12 
GeneralMy vote of 3 Pinmemberjasonbaisden19-Nov-13 6:36 
GeneralRe: My vote of 3 PinprofessionalAfazal MD 310420919-Nov-13 15:45 
QuestionSamples Pinmemberkiquenet.com18-Nov-13 19:53 
AnswerRe: Samples PinprofessionalAfazal MD 310420919-Nov-13 15:47 
QuestionWhat does this say about the .NET community? Pinmemberjmueller18-Nov-13 8:32 
AnswerRe: What does this say about the .NET community? Pinmemberjasonbaisden19-Nov-13 6:34 
GeneralRe: What does this say about the .NET community? PinprofessionalAfazal MD 310420919-Nov-13 15:49 
GeneralRe: What does this say about the .NET community? Pinmemberjmueller21-Nov-13 13:31 
I get that not everybody can upgrade right away, but surely all the articles about what's new in MVC 4 that ever needed to be written, were already written a year ago. There's nothing new about MVC 4 now that wasn't new about MVC 4 in August 2012. Why write new articles about something that hasn't changed in over a year, and was already thoroughly documented back then?
 
Also, there's nothing whatsoever about MVC 5 or Visual Studio 2013 that prevents you from supporting IE8. I speak from sad experience, because I would really love an excuse to dump IE8.

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 17 Nov 2013
Article Copyright 2013 by Afazal MD 3104209
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid