Skip to content

Async suffix for controller action names will be trimmed by default #14716

Closed
@analogrelay

Description

@analogrelay

Async suffix for controller action names will be trimmed by default

As part of addressing dotnet/aspnetcore#4849, ASP.NET Core MVC will trim the suffix Async from action names by default. This affects routing and link generation.

Version introduced

3.0

Old behavior

Consider the following ASP.NET Core MVC controller:

public class ProductController : Controller
{
    public async IActionResult ListAsync()
    {
        var model = await DbContext.Products.ToListAsync();
        return View(model);
    }
}

Prior to 3.0, the action will be routeable via Product/ListAsync. Link generation would require specifying the Async suffix e.g.

<a asp-controller="Product" asp-action="ListAsync">List</a>

New behavior

In 3.0, the action will be routeable via Product/List and link generation would require not specifying the Async suffix e.g.

<a asp-controller="Product" asp-action="List">List</a>

This change does not affect names specified using the ActionNameAttribute.

This behavior can be disabled by setting MvcOptions.SuppressAsyncSuffixInActionNames to false as part of the application startup:

services.AddMvc(options =>
{
   options.SuppressAsyncSuffixInActionNames = false; 
});

Reason for change

Asynchronous .NET methods, by convention, end with the Async suffix. However, when a method defines an MVC action, it is generally not desirable to have the Async suffix.

Recommended action

If your application depends on MVC actions retaining the Async suffix in the name, you can:

  • Use ActionNameAttribute to preserve the original name
  • Disable this renaming entirely by setting MvcOptions.SuppressAsyncSuffixInActionNames to false as part of the application startup:
services.AddMvc(options =>
{
   options.SuppressAsyncSuffixInActionNames = false; 
});

Category

  • ASP.NET Core

Affected APIs

Not detectable via API analysis


Issue metadata

  • Issue type: breaking-change

Metadata

Metadata

Assignees

Labels

breaking-changeIndicates a .NET Core breaking change

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions