Difference between ASP.NET MVC 3 and 4?

asp.net Mvcasp.net Mvc-3asp.net Mvc-4

asp.net Mvc Problem Overview


Is there a comprehensive list that explains all of the new features of MVC4 and what all has changed from MVC3?

(The release notes are not much helpful)

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Copied and pasted from MVC4 Release Notes:

Modern HTTP programming model: Directly access and manipulate HTTP requests and responses in your Web APIs using a new, strongly typed HTTP object model. The same programming model and HTTP pipeline is symmetrically available on the client through the new HttpClient type.

Full support for routes: ASP.NET Web API supports the full set of route capabilities of ASP.NET Routing, including route parameters and constraints. Additionally, use simple conventions to map actions to HTTP methods.

Content negotiation: The client and server can work together to determine the right format for data being returned from a web API. ASP.NET Web API provides default support for XML, JSON, and Form URL-encoded formats and you can extend this support by adding your own formatters, or even replace the default content negotiation strategy.

Model binding and validation: Model binders provide an easy way to extract data from various parts of an HTTP request and convert those message parts into .NET objects which can be used by the Web API actions. Validation is also performed on action parameters based on data annotations.

Filters: ASP.NET Web API supports filters including well-known filters such as the [Authorize] attribute. You can author and plug in your own filters for actions, authorization and exception handling.

Query composition: Use the [Queryable] filter attribute on an action that returns IQueryable to enable support for querying your web API via the OData query conventions.

Improved testability: Rather than setting HTTP details in static context objects, web API actions work with instances of HttpRequestMessage and HttpResponseMessage. Create a unit test project along with your Web API project to get started quickly writing unit tests for your Web API functionality.

Code-based configuration: ASP.NET Web API configuration is accomplished solely through code, leaving your config files clean. Use the provide service locator pattern to configure extensibility points.

Improved support for Inversion of Control (IoC) containers: ASP.NET Web API provides great support for IoC containers through an improved dependency resolver abstraction

Self-host: Web APIs can be hosted in your own process in addition to IIS while still using the full power of routes and other features of Web API.

Create custom help and test pages: You now can easily build custom help and test pages for your web APIs by using the new IApiExplorer service to get a complete runtime description of your web APIs.

Monitoring and diagnostics: ASP.NET Web API now provides light weight tracing infrastructure that makes it easy to integrate with existing logging solutions such as System.Diagnostics, ETW and third party logging frameworks. You can enable tracing by providing an ITraceWriter implementation and adding it to your web API configuration.

Link generation: Use the ASP.NET Web API UrlHelper to generate links to related resources in the same application.

Web API project template: Select the new Web API project form the New MVC 4 Project wizard to quickly get up and running with ASP.NET Web API.

Scaffolding: Use the Add Controller dialog to quickly scaffold a web API controller based on an Entity Framework based model type.

Solution 2 - asp.net Mvc

Copy and paste from Whats new in MVC4 - MVC3 Vs MVC4

> Whats new in MVC4 - MVC3 Vs 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)
    });
}

Hope this helps. Thanks

Solution 3 - asp.net Mvc

Solution 4 - asp.net Mvc

MVC 3

  1. Integrated Scaffolding system extensible via NuGet
  2. HTML 5 enabled project templates
  3. Expressive Views including the new Razor View Engine
  4. Powerful hooks with Dependency Injection and Global Action Filters
  5. Rich JavaScript support with unobtrusive JavaScript, jQuery Validation, and JSON binding

MVC 4

  1. ASP.NET Web API
  2. Refreshed and modernized default project templates
  3. New mobile project template
  4. Many new features to support mobile apps
  5. Enhanced support for asynchronous methods

Ref : http://dotnet-developers-cafe.blogspot.in/2013/09/difference-between-aspnet-mvc-3-and-mvc.html

Solution 5 - asp.net Mvc

Solution 6 - asp.net Mvc

One of the important feature introduced in MVC 4.0 was of Asynchronous controllers which enables to write the asynchronous action methods. Asynchronous controller allows an operation to get performed without making the working thread idle.

When an asynchronous action is invoked, the following steps occur:

The Web server gets a thread from the thread pool (the worker thread) and schedules it to handle an incoming request. This worker thread initiates an asynchronous operation. The worker thread is returned to the thread pool to service another Web request. When the asynchronous operation is complete, it notifies ASP.NET. The Web server gets a worker thread from the thread pool (which might be a different thread from the thread that started the asynchronous operation) to process the remainder of the request, including rendering the response.

Converting Synchronous Action Methods to Asynchronous Action Methods

Following is the example of synchronous action method and the its asynchronous equivalent version.

Synchronous Controller:

 public class TestController : Controller
 {
   public ActionResult Index()
    {
     return View(); 
    }
 }

Asynchronous variant of above operation:

public class TestController : AsyncController
{
   public void IndexAsync()
   {
    return View();
   }

  public ActionResult IndexCompleted()
  {
   return View();
  }
}

Steps:

  • Synchronous Controllers are the classes derived from the Controller class to implement an AsyncController instead of deriving the controller from Controller, derive it from AsyncController class. Controllers that derive from AsyncController enable ASP.NET to process asynchronous requests, and they can still service synchronous action methods.

  • Corresponding to the synchronous action method in Synchronous controller you need to create two methods for the action in asynchronous controller.First method that initiates the asynchronous process must have a name that consists of the action and the suffix "Async". The other method that is invoked when the asynchronous process finishes (the callback method) must have a name that consists of the action and the suffix "Completed".

In the above sample example, the Index action has been turned into two methods in asynchronous controller: IndexAsync and IndexCompleted.

The IndexAsync method returns void while the IndexCompleted method returns an ActionResult instance. Although the action consists of two methods, it is accessed using the same URL as for a synchronous action method (for example, Controller/Index).

Note the following about asynchronous action methods:

If the action name is Sample, the framework will look for SampleAsync and SampleCompleted methods.

View pages should be named Sample.aspx rather than SampleAsync.aspx or SampleCompleted.aspx. (The action name is Sample, not SampleAsync)

A controller cannot contain an asynchronous method named SampleAsync and a synchronous method named Sample. If it does, an AmbiguousMatchException exception is thrown because the SampleAsync action method and the Sample action method have the same request signature.

For more details click here : http://www.counsellingbyabhi.com/2014/05/asynchronous-controllers-in-aspnet-mvc.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAkshat Jiwan SharmaView Question on Stackoverflow
Solution 1 - asp.net MvcJSK NSView Answer on Stackoverflow
Solution 2 - asp.net MvcDisplay NameView Answer on Stackoverflow
Solution 3 - asp.net MvcAfazalView Answer on Stackoverflow
Solution 4 - asp.net MvcMukesh KumarView Answer on Stackoverflow
Solution 5 - asp.net MvcRahulView Answer on Stackoverflow
Solution 6 - asp.net MvcAbhishek GahloutView Answer on Stackoverflow