Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing

I had an Asp.Net core 2.2 project.

Recently, I changed the version from .net core 2.2 to .net core 3.0 Preview 8. After this change I see this warning message:

using 'UseMvc' to configure MVC is not supported while using Endpoint Routing. To continue using 'UseMvc', please set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices'.

I understand that by setting EnableEndpointRouting to false I can solve the issue, but I need to know what is the proper way to solve it and why Endpoint Routing does not need UseMvc() function.

like image 339
Nick Mehrdad Babaki Avatar asked Oct 15 '22 15:10

Nick Mehrdad Babaki


People also ask

What is MVC endpoint routing?

In other words, endpoint routing decouples the route matching and endpoint dispatching functions, giving you the flexibility to combine different middleware (MVC, CORS, Razor Pages, Blazor, etc.) in your applications. To continue reading this article register now. Get Free Access.

What is Endpoint routing?

The Endpoint Routing is the Process by which ASP.NET Core inspects the incoming HTTP requests and maps them to applications executable Endpoint. We define the Endpoint during the application startup. The Routing Module then matches the incoming URL to an Endpoint and dispatches the request to it.

What is EnableEndpointRouting?

Gets or sets a value that determines if routing should use endpoints internally, or if legacy routing logic should be used. Endpoint routing is used to match HTTP requests to MVC actions, and to generate URLs with IUrlHelper. public: property bool EnableEndpointRouting { bool get(); void set(bool value); }; C# Copy.

What are the differences between app UseRouting () and APP UseEndpoints ()?

UseRouting: Matches request to an endpoint. UseEndpoints: Execute the matched endpoint. It decouples the route matching and resolution functionality from the endpoint executing functionality, which until now was all bundled in with the MVC middleware.


1 Answers

I found the solution, in the following official documentation "Migrate from ASP.NET Core 2.2 to 3.0":

There are 3 approaches:

  1. Replace UseMvc or UseSignalR with UseEndpoints.

In my case, the result looked like that

  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}");
        });

    }
}

OR
2. Use AddControllers() and UseEndpoints()

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();
        });

    }
}

OR
3. Disable endpoint Routing. As the exception message suggests and as mentioned in the following section of documentation: use mvcwithout endpoint routing


services.AddMvc(options => options.EnableEndpointRouting = false);
//OR
services.AddControllers(options => options.EnableEndpointRouting = false);
like image 255
Sergii Zhuravskyi Avatar answered Oct 17 '22 04:10

Sergii Zhuravskyi