Whats new in MVC4 - MVC3 Vs MVC4

18372
Views
1
Answers

Whats new in MVC4 ? What features are added and what is the difference?

edit | flag
Mike Carpenter
Asked on: Feb 07, 2012 at 10:10AM

1 Answers

123
3
123

The White papers of MVC4 lists the below features of MVC4

Enhancements to Default Project Templates

The template that is used to create new ASP.NET MVC 4 projects has been updated to create a more modern-looking website

Mobile Project Template

If you’re starting a new project and want to create a site specifically for mobile and tablet browsers, you can use the new Mobile Application project template. This is based on jQuery Mobile, an open-source library for building touch-optimized UI

Display Modes

The new Display Modes feature lets an application select views depending on the browser that's making the request. For example, if a desktop browser requests the Home page, the application might use the Views\Home\Index.cshtml template. If a mobile browser requests the Home page, the application might return the Views\Home\Index.mobile.cshtml template.

DisplayModes.Modes.Insert(0, new DefaultDisplayMode("iPhone")
{
   
ContextCondition = (context => context.Request.UserAgent.IndexOf
       
("iPhone", StringComparison.OrdinalIgnoreCase) >= 0)
 
});

jQuery Mobile, the View Switcher, and Browser Overriding

jQuery Mobile is an open source library for building touch-optimized web UI. If you want to use jQuery Mobile with an ASP.NET MVC 4 application, you can download and install a NuGet package that helps you get started. To install it from the Visual Studio Package Manager Console, type the following command:

Install-Package jQuery.Mobile.MVC

This installs jQuery Mobile and some helper files, including the following:

Views/Shared/Layout.Mobile.cshtml, which is a jQuery Mobile-based layout.
A view-switcher component, which consists of the Views/Shared/
ViewSwitcher.cshtml partial view and the ViewSwitcherController.cs controller.
After you install the package, run your application using a mobile browser (or equivalent, like the Firefox User Agent Switcher add-on). You'll see that your pages look quite different, because jQuery Mobile handles layout and styling. To take advantage of this, you can do the following

If visitors click the link, they’re switched to the desktop version of the same page.

Because your desktop layout will not include a view switcher by default, visitors won't have a way to get to mobile mode. To enable this, add the following reference to _ViewSwitcher to your desktop layout, just inside the element:

@Html.Partial("_ViewSwitcher")
...

Browser Overriding is a core feature of ASP.NET MVC 4 and is available even if you don't install the jQuery.Mobile.MVC package. However, it affects only view, layout, and partial-view selection — it does not affect any other ASP.NET feature that depends on the Request.Browser object.

Recipes for Code Generation in Visual Studio

The new Recipes feature enables Visual Studio to generate solution-specific code based on packages that you can install using NuGet. The Recipes framework makes it easy for developers to write code-generation plugins, which you can also use to replace the built-in code generators for Add Area, Add Controller, and Add View. Because recipes are deployed as NuGet packages, they can easily be checked into source control and shared with all developers on the project automatically. They are also available on a per-solution basis.

Task Support for Asynchronous Controllers

You can now write asynchronous action methods as single methods that return an object of type Task or Task.

For example, if you're using Visual C# 5 (or using the Async CTP), you can create an asynchronous action method that looks like the following:

public async Task Index(string city) {
   
var newsService = new NewsService();
   
var sportsService = new SportsService();

   
return View("Common",
       
new PortalViewModel {
       
NewsHeadlines = await newsService.GetHeadlinesAsync(),
       
SportsScores = await sportsService.GetScoresAsync()
   
});
}

In the previous action method, the calls to newsService.GetHeadlinesAsync and sportsService.GetScoresAsync are called asynchronously and do not block a thread from the thread pool.

Asynchronous action methods that return Task instances can also support timeouts. To make your action method cancellable, add a parameter of type CancellationToken to the action method signature. The following example shows an asynchronous action method that has a timeout of 2500 milliseconds and that displays a TimedOut view to the client if a timeout occurs.

[AsyncTimeout(2500)]
 
[HandleError(ExceptionType = typeof(TaskCanceledException), View = "TimedOut")]
public async Task Index(string city,
   
CancellationToken cancellationToken) {
   
var newsService = new NewsService();
   
var sportsService = new SportsService();

   
return View("Common",
       
new PortalViewModel {
       
NewsHeadlines = await newsService.GetHeadlinesAsync(cancellationToken),
       
SportsScores = await sportsService.GetScoresAsync(cancellationToken)
   
});
}
edit | flag
Gokul A
Answered on: Feb 07, 2012 at 10:22AM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags