Coder Perfect

When utilizing Endpoint Routing, using ‘UseMvc’ to configure MVC is not supported.

Problem

I was working on an Asp.Net core 2.2 project at the time.

I recently upgraded from.net core 2.2 to.net core 3.0 Preview 8 and am pleased with the results. Following this adjustment, I see the following warning message:

I realize that turning off EnableEndpointRouting solves the problem, but I’m not sure what the appropriate solution is or why Endpoint Routing doesn’t require the UseMvc() function.

Asked by Nick Mehrdad Babaki

Solution #1

In the official documentation “Migrate from ASP.NET Core 2.2 to 3.0,” I discovered the solution:

Three options are available:

In my case, the outcome was as follows:

  public class Startup
{

    public void ConfigureServices(IServiceCollection services)
    {
        //Old Way
        services.AddMvc();
        // New Ways
        //services.AddRazorPages();
    }


    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseStaticFiles();
        app.UseRouting();
        app.UseCors();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}");
        });

    }
}
public class Startup
{

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
    }


    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseStaticFiles();
        app.UseRouting();
        app.UseCors();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

    }
}

services.AddMvc(options => options.EnableEndpointRouting = false);
//OR
services.AddControllers(options => options.EnableEndpointRouting = false);

Answered by Sergii Zhuravskyi

Solution #2

This method works for me (included in Startup.cs > ConfigureServices function):

services.AddMvc(option => option.EnableEndpointRouting = false)

Answered by Bonaventura72

Solution #3

In general, you should use EnableEndpointRouting instead of UseMvc, and you can find detailed instructions on how to enable EnableEndpointRouting in Update routing starting code.

UseMvc employs IRouter-based logic, whereas EnableEndpointRouting employs endpoint-based logic. They each have their own rationale, which can be found below:

if (options.Value.EnableEndpointRouting)
{
    var mvcEndpointDataSource = app.ApplicationServices
        .GetRequiredService<IEnumerable<EndpointDataSource>>()
        .OfType<MvcEndpointDataSource>()
        .First();
    var parameterPolicyFactory = app.ApplicationServices
        .GetRequiredService<ParameterPolicyFactory>();

    var endpointRouteBuilder = new EndpointRouteBuilder(app);

    configureRoutes(endpointRouteBuilder);

    foreach (var router in endpointRouteBuilder.Routes)
    {
        // Only accept Microsoft.AspNetCore.Routing.Route when converting to endpoint
        // Sub-types could have additional customization that we can't knowingly convert
        if (router is Route route && router.GetType() == typeof(Route))
        {
            var endpointInfo = new MvcEndpointInfo(
                route.Name,
                route.RouteTemplate,
                route.Defaults,
                route.Constraints.ToDictionary(kvp => kvp.Key, kvp => (object)kvp.Value),
                route.DataTokens,
                parameterPolicyFactory);

            mvcEndpointDataSource.ConventionalEndpointInfos.Add(endpointInfo);
        }
        else
        {
            throw new InvalidOperationException($"Cannot use '{router.GetType().FullName}' with Endpoint Routing.");
        }
    }

    if (!app.Properties.TryGetValue(EndpointRoutingRegisteredKey, out _))
    {
        // Matching middleware has not been registered yet
        // For back-compat register middleware so an endpoint is matched and then immediately used
        app.UseEndpointRouting();
    }

    return app.UseEndpoint();
}
else
{
    var routes = new RouteBuilder(app)
    {
        DefaultHandler = app.ApplicationServices.GetRequiredService<MvcRouteHandler>(),
    };

    configureRoutes(routes);

    routes.Routes.Insert(0, AttributeRouting.CreateAttributeMegaRoute(app.ApplicationServices));

    return app.UseRouter(routes.Build());
}

EndpointMiddleware is used by EnableEndpointRouting to route requests to endpoints.

Answered by Edward

Solution #4

The problem was caused by modifications to the.NET Core framework, which I discovered. Using MVC in the latest.NET Core 3.0 release requires explicit opt-in.

This problem is mainly noticeable when migrating from an older version of.NET Core (2.2 or preview 3.0) to a newer version of.NET Core 3.0.

If you’re upgrading from 2.2 to 3.0, please apply the code below to fix the problem.

services.AddMvc(options => options.EnableEndpointRouting = false);

If you’re working with the.NET Core 3.0 template,

services.AddControllers(options => options.EnableEndpointRouting = false);

After fixing the ConfigServices method as shown below,

Thank You

Answered by Akash Limbani

Solution #5

ASP.NET 5.0 comes with Endpoint Routing turned off by default.

Simply set up as described in the Startup procedure.

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(options => options.EnableEndpointRouting = false);
    }

For me, it worked.

Answered by Md. Nazmul Nadim

Post is based on https://stackoverflow.com/questions/57684093/using-usemvc-to-configure-mvc-is-not-supported-while-using-endpoint-routing