Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No overload for method 'UseRouting' takes 1 arguments

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

like image 993
Mitch Avatar asked May 15 '19 19:05

Mitch


People also ask

What does no overload for method mean in C#?

No overload for method 'method' takes 'number' arguments. A call was made to a class method, but no definition of the method takes the specified number of arguments.

What is use of UseRouting and UseEndpoints in startup configure method?

UseRouting adds route matching to the middleware pipeline. This middleware looks at the set of endpoints defined in the app, and selects the best match based on the request. UseEndpoints adds endpoint execution to the middleware pipeline. It runs the delegate associated with the selected endpoint.


2 Answers

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.

like image 155
poke Avatar answered Sep 28 '22 12:09

poke


After some digging, I found a solution for this problem. Since dotnet core 3.0 requires some extra action, I will explain what I did to make this work. Firstable, in the ConfigureServices() method (in Startup.cs), remove:

services.AddMvc().AddNewtonsoftJson();

At the top of this method (under services.Configure<>), add the following lines:

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

Next, in the Configure() method, add app.UseRouting() before app.UseAuthentication() and app.UseAuthorization(); and at the bottom of this method replace

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

with:

    app.UseEndpoints(endpoints => {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
        endpoints.MapRazorPages();
    });
like image 23
Mitch Avatar answered Sep 28 '22 10:09

Mitch