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:

MVC Framework

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.

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.

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.

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.

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

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.

Article is a good, lite-weight summary of the new MVC 4 features, but lacks substantial details for anything more in depth. If only one item in the article could be expounded upon, I would choose the Task/await key words and usage.

I understand what you're saying, but not everyone is on the bleeding edge of technology. Our company is still on ASP.NET MVC 3 and eyeing a move to MVC 4. We're stuck in a rut of supporting I.E. 8, which means moving to MVC 5 and Visual Studio 2013 is not possible. For me, the article was a good, though brief, summary of the new features in MVC 4. I would have loved some more code samples to illustrate some of these features (especially the Task & await keywords), but, as a lite-weight primer, this article was informative and a quick read.

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.