"
ASP.NET (snapshot 2017) Microsoft documentation and samples

ASP.NET MVC 4

This document describes the release of ASP.NET MVC 4 Beta for Visual Studio 2010.

[!NOTE] This is not the most current release. The ASP.NET MVC 4 RC release notes are available here.

## Installation Notes

ASP.NET MVC 4 Beta for Visual Studio 2010 can be installed from the ASP.NET MVC 4 home page using the Web Platform Installer.

You must uninstall any previously installed previews of ASP.NET MVC 4 prior to installing ASP.NET MVC 4 Beta.

This release is not compatible with the .NET Framework 4.5 Developer Preview. You must uninstall the .NET 4.5 Developer Preview before installing the ASP.NET MVC 4 Beta.

ASP.NET MVC 4 can be installed and can run side-by-side with ASP.NET MVC 3.

## Documentation

Documentation for ASP.NET MVC is available on the MSDN website at the following URL:

https://go.microsoft.com/fwlink/?LinkID=243043

Tutorials and other information about ASP.NET MVC are available on the MVC 4 page of the ASP.NET website (https://www.asp.net/mvc/mvc4).

## Support

This is a preview release and is not officially supported. If you have questions about working with this release, post them to the ASP.NET MVC forum (https://forums.asp.net/1146.aspx), where members of the ASP.NET community are frequently able to provide informal support.

## Software Requirements

The ASP.NET MVC 4 components for Visual Studio require PowerShell 2.0 and either Visual Studio 2010 with Service Pack 1 or Visual Web Developer Express 2010 with Service Pack 1.

## Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4

ASP.NET MVC 4 can be installed side by side with ASP.NET MVC 3 on the same computer, which gives you flexibility in choosing when to upgrade an ASP.NET MVC 3 application to ASP.NET MVC 4.

The simplest way to upgrade is to create a new ASP.NET MVC 4 project and copy all the views, controllers, code, and content files from the existing MVC 3 project to the new project and then to update the assembly references in the new project to match the old project. If you have made changes to the Web.config file in the MVC 3 project, you must also merge those changes into the Web.config file in the MVC 4 project.

To manually upgrade an existing ASP.NET MVC 3 application to version 4, do the following:

  1. In all Web.config files in the project (there is one in the root of the project, one in the Views folder, and one in the Views folder for each area in your project), replace every instance of the following text:

    [!code-consoleMain]

       1:  System.Web.Mvc, Version=3.0.0.0
       2:  System.Web.WebPages, Version=1.0.0.0
       3:  System.Web.Helpers, Version=1.0.0.0
       4:  System.Web.WebPages.Razor, Version=1.0.0.0

    with the following corresponding text:

    [!code-consoleMain]
       1:  System.Web.Mvc, Version=4.0.0.0
       2:  System.Web.WebPages, Version=2.0.0.0
       3:  System.Web.Helpers, Version=2.0.0.0,
       4:   System.Web.WebPages.Razor, Version=2.0.0.0,
  2. In the root Web.config file, update the webPages:Version element to “2.0.0.0” and add a new PreserveLoginUrl key that has the value “true”:

    [!code-xmlMain]
       1:  <appSettings>
       2:    <add key="webpages:Version" value="2.0.0.0" />
       3:    <add key="PreserveLoginUrl" value="true" />
       4:  </appSettings>
  3. In Solution Explorer, delete the reference to System.Web.Mvc (which points to the version 3 DLL). Then add a reference to System.Web.Mvc (v4.0.0.0). In particular, make the following changes to update the assembly references. Here are the details:

    1. In Solution Explorer, delete the references to the following assemblies:

      • System.Web.Mvc(v3.0.0.0)
      • System.Web.WebPages(v1.0.0.0)
      • System.Web.Razor(v1.0.0.0)
      • System.Web.WebPages.Deployment(v1.0.0.0)
      • System.Web.WebPages.Razor(v1.0.0.0)
    2. Add a references to the following assemblies:

      • System.Web.Mvc(v4.0.0.0)
      • System.Web.WebPages(v2.0.0.0)
      • System.Web.Razor(v2.0.0.0)
      • System.Web.WebPages.Deployment(v2.0.0.0)
      • System.Web.WebPages.Razor(v2.0.0.0)
  4. In Solution Explorer, right-click the project name and then select Unload Project. Then right-click the name again and select Edit ProjectName.csproj.
  5. Locate the ProjectTypeGuids element and replace {E53F8FEA-EAE0-44A6-8774-FFD645390401} with {E3E379DF-F4C6-4180-9B81-6769533ABE47}.
  6. Save the changes, close the project (.csproj) file you were editing, right-click the project, and then select Reload Project.
  7. If the project references any third-party libraries that are compiled using previous versions of ASP.NET MVC, open the root Web.config file and add the following three bindingRedirect elements under the configuration section:

    [!code-xmlMain]

       1:  <configuration>
       2:    <!--... elements deleted for clarity ...-->
       3:   
       4:    <runtime>
       5:      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       6:        <dependentAssembly>
       7:          <assemblyIdentity name="System.Web.Helpers" 
       8:               publicKeyToken="31bf3856ad364e35" />
       9:          <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
      10:        </dependentAssembly>
      11:        <dependentAssembly>
      12:          <assemblyIdentity name="System.Web.Mvc" 
      13:               publicKeyToken="31bf3856ad364e35" />
      14:          <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="4.0.0.0"/>
      15:        </dependentAssembly>
      16:        <dependentAssembly>
      17:          <assemblyIdentity name="System.Web.WebPages" 
      18:               publicKeyToken="31bf3856ad364e35" />
      19:          <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
      20:        </dependentAssembly>
      21:      </assemblyBinding>
      22:    </runtime>
      23:  </configuration>

## New Features in ASP.NET MVC 4 Beta

This section describes features that have been introduced in the ASP.NET MVC 4 Beta release.

### ASP.NET Web API

ASP.NET MVC 4 now includes ASP.NET Web API, a new framework for creating HTTP services that can reach a broad range of clients including browsers and mobile devices. ASP.NET Web API is also an ideal platform for building RESTful services.

ASP.NET Web API includes support for the following features:

For more details on ASP.NET Web API please visit https://www.asp.net/web-api.

### ASP.NET Single Page Application

ASP.NET MVC 4 now includes an early preview of the experience for building single page applications with significant client-side interactions using JavaScript and Web APIs. This support includes:

For more details on the Single Page Application support in ASP.NET MVC 4 please visit https://www.asp.net/single-page-application.

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

In addition to cosmetic improvements, there’s improved functionality in the new template. The template employs a technique called adaptive rendering to look good in both desktop browsers and mobile browsers without any customization.

To see adaptive rendering in action, you can use a mobile emulator or just try resizing the desktop browser window to be smaller. When the browser window gets small enough, the layout of the page will change.

Another enhancement to the default project template is the use of JavaScript to provide a richer UI. The Login and Register links that are used in the template are examples of how to use the jQuery UI Dialog to present a rich login screen:

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

This template contains the same application structure as the Internet Application template (and the controller code is virtually identical), but it’s styled using jQuery Mobile to look good and behave well on touch-based mobile devices. To learn more about how to structure and style mobile UI, see the jQuery Mobile project website.

If you already have a desktop-oriented site that you want to add mobile-optimized views to, or if you want to create a single site that serves differently styled views to desktop and mobile browsers, you can use the new Display Modes feature. (See the next section.)

### 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.cshtml template. If a mobile browser requests the Home page, the application might return the Views.mobile.cshtml template.

Layouts and partials can also be overridden for particular browser types. For example:

If you want to create more specific views, layouts, or partial views for other devices, you can register a new DefaultDisplayMode instance to specify which name to search for when a request satisfies particular conditions. For example, you could add the following code to the Application_Start method in the Global.asax file to register the string “iPhone” as a display mode that applies when the Apple iPhone browser makes a request:

[!code-csharpMain]

   1:  DisplayModeProvider.Instance.Modes.Insert(0, new
   2:  DefaultDisplayMode("iPhone")
   3:  {
   4:      ContextCondition = (context => context.GetOverriddenUserAgent().IndexOf
   5:          ("iPhone", StringComparison.OrdinalIgnoreCase) >= 0)
   6:   });

After this code runs, when an Apple iPhone browser makes a request, your application will use the Views\_Layout.iPhone.cshtml layout (if it exists).

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

[!code-powershellMain]

   1:  Install-Package jQuery.Mobile.MVC

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

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:

A convention for mobile-optimized web pages is to add a link whose text is something like Desktop view or Full site mode that lets users switch to a desktop version of the page. The jQuery.Mobile.MVC package includes a sample view-switcher component for this purpose. It’s used in the default Views\_Layout.Mobile.cshtml view, and it looks like this when the page is rendered:

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 <body> element:

[!code-cshtmlMain]

   1:  <body>
   2:      @Html.Partial("_ViewSwitcher")
   3:      ...

The view switcher uses a new feature called Browser Overriding. This feature lets your application treat requests as if they were coming from a different browser (user agent) than the one they’re actually from. The following table lists the methods that Browser Overriding provides.

HttpContext.SetOverriddenBrowser(userAgentString) Overrides the request’s actual user agent value using the specified user agent.
HttpContext.GetOverriddenUserAgent() Returns the request’s user agent override value, or the actual user agent string if no override has been specified.
HttpContext.GetOverriddenBrowser() Returns an HttpBrowserCapabilitiesBase instance that corresponds to the user agent currently set for the request (actual or overridden). You can use this value to get properties such as IsMobileDevice.
HttpContext.ClearOverriddenBrowser() Removes any overridden user agent for the current request.

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.

By default, the user-agent override is stored using a cookie. If you want to store the override elsewhere (for example, in a database), you can replace the default provider (BrowserOverrideStores.Current). Documentation for this provider will be available to accompany a later release of ASP.NET MVC.

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

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:

[!code-csharpMain]

   1:  public async Task<ActionResult> Index(string city) {
   2:      var newsService = new NewsService();
   3:      var sportsService = new SportsService();
   4:      
   5:      return View("Common",
   6:          new PortalViewModel {
   7:          NewsHeadlines = await newsService.GetHeadlinesAsync(),
   8:          SportsScores = await sportsService.GetScoresAsync()
   9:      });
  10:  }

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.

[!code-csharpMain]

   1:  [AsyncTimeout(2500)]
   2:  [HandleError(ExceptionType = typeof(TaskCanceledException), View = "TimedOut")]
   3:  public async Task<ActionResult> Index(string city,
   4:      CancellationToken cancellationToken) {
   5:      var newsService = new NewsService();
   6:      var sportsService = new SportsService();
   7:     
   8:      return View("Common",
   9:          new PortalViewModel {
  10:          NewsHeadlines = await newsService.GetHeadlinesAsync(cancellationToken),
  11:          SportsScores = await sportsService.GetScoresAsync(cancellationToken)
  12:      });
  13:  }

### Azure SDK

ASP.NET MVC 4 Beta supports the September 2011 1.5 release of the Windows Azure SDK.

## Known Issues and Breaking Changes

The generated namespace omits everything after the root namespace. For example, the namespace generated is RootNamespace but should be RootNamespace.Areas.AreaName.Controllers . - Breaking changes in the Razor View Engine. As part of a rewrite of the Razor parser, the following types were removed from System.Web.Mvc.Razor:

- *ModelSpan*
- *MvcVBRazorCodeGenerator*
- *MvcCSharpRazorCodeGenerator*
- *MvcVBRazorCodeParser*

The following methods were also removed:

- *MvcCSharpRazorCodeParser.ParseInheritsStatement(System.Web.Razor.Parser.CodeBlockInfo)*
- *MvcWebPageRazorHost.DecorateCodeGenerator(System.Web.Razor.Generator.RazorCodeGenerator)*
- *MvcVBRazorCodeParser.ParseInheritsStatement(System.Web.Razor.Parser.CodeBlockInfo)*


Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/whitepapers/mvc4-beta-release-notes.htm
< THANKS ME>