No overload for method 'UseRouting' takes 1 arguments

Mitch picture Mitch · May 15, 2019 · Viewed 15.2k times · Source

I just updated to ASP.NET Core 3 Preview 5, now when I open my solution and try to build it throws the error 'No overload for method 'UseRouting' takes 1 arguments' in the Startup.cs file in the Configure() at the following code:

    app.UseRouting(routes => {
        routes.MapControllerRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
        routes.MapRazorPages();
    });

I did read some documentation on the Microsoft docs and tried replacing the above code with:

    app.UseEndpoints(endpoints => {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
        endpoints.MapRazorPages();
    });

However, during build time that throws an System.InvalidOperationException with the following context:

'EndpointRoutingMiddleware matches endpoints setup by EndpointMiddleware and so must be added to the request execution pipeline before EndpointMiddleware. Please add EndpointRoutingMiddleware by calling 'IApplicationBuilder.UseRouting' inside the call to 'Configure(...)' in the application startup code.'

I tried replacing the following line in the ConfigureServices method:

    services.AddMvc()
        .AddNewtonsoftJson();

width:

    services.AddControllersWithViews()
        .AddNewtonsoftJson();
    services.AddRazorPages();

This does not throw errors anymore but my page is blank when it finishes loading. Who can help me solve this issue?

For my solution I use the following packages:

 <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0-preview5.19227.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0-preview5.19227.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0-preview5.19227.9" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />

The TargetFramework of my solution is netcoreapp3.0

Answer

poke picture poke · May 16, 2019

To quote the error message again:

EndpointRoutingMiddleware matches endpoints setup by EndpointMiddleware and so must be added to the request execution pipeline before EndpointMiddleware. Please add EndpointRoutingMiddleware by calling 'IApplicationBuilder.UseRouting' inside the call to 'Configure(...)' in the application startup code.

ASP.NET Core 3 uses a refined endpoint routing which will generally give more control about routing within the application. Endpoint routing works in two separate steps:

  • In a first step, the requested route is matched agains the configured routes to figure out what route is being accessed.
  • In a final step, the determined route is being evaluated and the respective middleware, e.g. MVC, is called.

These are two separate steps to allow other middlewares to act between those points. That allows those middlewares to utilize the information from endpoint routing, e.g. to handle authorization, without having to execute as part of the actual handler (e.g. MVC).

The two steps are set up by app.UseRouting() and app.UseEndpoints(). The former will register the middleware that runs the logic to determine the route. The latter will then execute that route.

If you read the error message carefully, between the somewhat confusing usage of EndpointRoutingMiddleware and EndpointMiddleware, it will tell you to add UseRouting() inside of the Configure method. So basically, you forgot to invoke the first step of endpoint routing.

So your Configure should (for example) look like this:

app.UseRouting();

app.UseAuthentication();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
});

The routing migration to 3.0 is also documented in the migration guide for 3.0.