What's New in ASP.NET MVC 5.1

by Microsoft

This topic describes what's new for ASP.NET Web MVC 5.1.

Software Requirements

Download

The runtime features are released as NuGet packages on the NuGet gallery. All the runtime packages follow the Semantic Versioning specification. The latest ASP.NET MVC 5.1 RTM package has the following version: "5.1.2". You can install or update these packages through NuGet. The release also includes corresponding localized packages on NuGet.

You can install or update to the released NuGet packages by using the NuGet Package Manager Console:

Install-Package Microsoft.AspNet.Mvc -Version 5.1.2

Documentation

Tutorials and other information about ASP.NET MVC 5.1 RTM are available from the ASP.NET web site ( https://www.asp.net).

New Features in ASP.NET MVC 5.1

Attribute routing improvements

Attribute routing now supports constraints, enabling versioning and header based route selection. Many aspects of attribute routes are now customizable via the IDirectRouteFactory interface and RouteFactoryAttribute class. The route prefix is now extensible via the IRoutePrefix interface and RoutePrefixAttribute class.

Enum support in views

  1. New @Html.EnumDropDownListFor() helper methods. These should be used like most of the HTML helpers with the caveat that the expression must evaluate to an enum type or a Nullable<T> where T is an enum type. Use EnumHelper.IsValidForEnumHelper() to check these requirements.
  2. New EnumHelper.GetSelectList() methods which return an IList<SelectListItem>. This is useful when you need to manipulate a select list prior to calling, for example, @Html.DropDownListFor(), or when you wish to display the names which @Html.EnumDropDownListFor() shows.

The following code shows these APIs.

@if (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata))
{
    @Html.EnumDropDownListFor(model => model, htmlAttributes: new { @class = "form-control" })
}
@if (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata))
{
    foreach (SelectListItem item in EnumHelper.GetSelectList(ViewData.ModelMetadata,
(Enum)Model)) { … }
}

You can see a complete example here.

Bootstrap support for editor templates

We now allow passing in HTML attributes in EditorFor as an anonymous object.

For example:

@Html.EditorFor(model => model, new { htmlAttributes = new { @class = "form-control" }, })

Unobtrusive validation for MinLengthAttribute and MaxLengthAttribute

Client-side validation for string and array types will now be supported for properties decorated with the MinLength and MaxLength attributes.

Supporting the ‘this' context in Unobtrusive Ajax

The callback functions (OnBegin, OnComplete, OnFailure, OnSuccess) will now be able to locate the invoking element via the this context. For example:

@Ajax.ActionLink("Click me", "AjaxAction", new AjaxOptions { UpdateTargetId = "foo", OnBegin = "OnClick" })

<script>
    function OnClick(jqXHR) {
        if ($(this).hasClass("foo")) {
            jqXHR.setRequestHeader("custom-header", "value");
        }
    }
</script>

Known Issues and Breaking Changes

Attribute Routing

Ambiguities in attribute routing matches will now report an error rather than choosing the first match.

Attribute routes are prohibited from using the {controller} parameter, and from using the {action} parameter on routes placed on actions. Uses of these parameters would very likely lead to ambiguities.

Scaffolding MVC/Web API into a project with 5.1 packages results in 5.0 packages for ones that don't already exist in the project

Updating NuGet packages for ASP.NET MVC 5.1 RTM does not update the Visual Studio tools such as ASP.NET scaffolding or the ASP.NET Web Application project template. They use the previous version of the ASP.NET runtime packages (5.0.0.0). As a result, the ASP.NET scaffolding will install the previous version (5.0.0.0) of the required packages, if they are not already available in your projects. However, the ASP.NET scaffolding in Visual Studio 2013 RTM or Update 1 does not overwrite the latest packages in your projects. If you use ASP.NET scaffolding after updating the packages of your projects to Web API 2.1 or ASP.NET MVC 5.1, make sure the versions of Web API and ASP.NET MVC are consistent.

Syntax Highlighting for Razor Views in Visual Studio 2013

If you update to ASP.NET MVC 5.1 RTM without updating Visual Studio 2013, you will not get Visual Studio editor support for syntax highlighting while editing the Razor views. You will need to update Visual Studio 2013 to get this support.

Type Renames

Some of the types used for attribute routing extensibility are renamed in 5.1 RTM.

Old Type Name (5.1 RC) New Type Name (5.1 RTM)
IDirectRouteProvider IDirectRouteFactory
RouteProviderAttribute RouteFactoryAttribute
DirectRouteProviderContext DirectRouteFactoryContext