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

New Features in ASP.NET MVC 4

Rate me:
Please Sign up or sign in to vote.
4.25/5 (21 votes)
17 Nov 2013CPOL5 min read 92.3K   29   13
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:

Image 1

Step 3: Project folder structure as below:

Image 2

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<actionresult>. .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:

C#
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.

C#
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.

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

Example:

C#
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:

Image 3

Step 2: Now Select Web API as below:

Image 4

Step 3: API controller class snap shot:

Image 5

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.

Image 6

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.

Practical ASP.Net MVC Interview Questions and Answers

Please go through the link ASP.Net Interview questions and answer

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)


Written By
Software Developer (Senior) Epicor Software
India India
16+ 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,Angular, Node, Type script and SQL server.

You can reach me @ mdafazal@gmail.com

Comments and Discussions

 
SuggestionTitle and Content dose not matched... Pin
Suvabrata Roy22-Sep-15 2:26
professionalSuvabrata Roy22-Sep-15 2:26 
QuestionGood work. Nice explanation. Pin
Imran Abdul Ghani22-Jul-15 5:44
Imran Abdul Ghani22-Jul-15 5:44 
QuestionMVC4 Features Pin
Member 110720903-Feb-15 10:25
Member 110720903-Feb-15 10:25 
Generalgood article Pin
sachin shelake27-Apr-14 7:08
sachin shelake27-Apr-14 7:08 
GeneralMy vote of 4 Pin
vmsrdy19-Nov-13 15:12
vmsrdy19-Nov-13 15:12 
GeneralMy vote of 3 Pin
jasonbaisden19-Nov-13 6:36
jasonbaisden19-Nov-13 6:36 
GeneralRe: My vote of 3 Pin
Afazal MD 310420919-Nov-13 15:45
professionalAfazal MD 310420919-Nov-13 15:45 
QuestionSamples Pin
kiquenet.com18-Nov-13 19:53
professionalkiquenet.com18-Nov-13 19:53 
AnswerRe: Samples Pin
Afazal MD 310420919-Nov-13 15:47
professionalAfazal MD 310420919-Nov-13 15:47 
QuestionWhat does this say about the .NET community? Pin
jmueller18-Nov-13 8:32
jmueller18-Nov-13 8:32 
AnswerRe: What does this say about the .NET community? Pin
jasonbaisden19-Nov-13 6:34
jasonbaisden19-Nov-13 6:34 
GeneralRe: What does this say about the .NET community? Pin
Afazal MD 310420919-Nov-13 15:49
professionalAfazal MD 310420919-Nov-13 15:49 
GeneralRe: What does this say about the .NET community? Pin
jmueller21-Nov-13 13:31
jmueller21-Nov-13 13:31 

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.