Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net core 3 , MVC , Using 'UseMvcWithDefaultRoute' to configure MVC is not supported while using Endpoint Routing

I am trying to create a simple project based on ASP.NET Core 3.

The MVC template for ASP.NET Core 2.2 has the following line inside the startup-class:

app.UseMvcWithDefaultRoute();

This line works perfectly in ASP.NET Core 2.2 and routing works, however, in ASP.NET Core 3.0 it doesn't compile and displays the following error

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

The question is: "How to configure routing in .net core version 3 for MVC application?"

like image 378
Sergii Zhuravskyi Avatar asked Oct 07 '19 08:10

Sergii Zhuravskyi


2 Answers

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

Thre are 3 approaches:

  1. Disable endpoint Routing.
(add in Startup.cs)

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

OR

  1. Replace UseMvc or UseSignalR with UseEndpoints.

In my case, the result looked like that

  public class Startup
{
    
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    
    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

  1. 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();
        });
        
    }
}
like image 72
Sergii Zhuravskyi Avatar answered Sep 23 '22 08:09

Sergii Zhuravskyi


This doesn't exist for ASP.NET Core 3 as you can see here it is only supported until 2.2.

You need to switch to app.UseMvc(); when registering full MVC pipeline.

For APIs you need to do the following

app.UseRouting();
app.UseEndpoints(builder => builder.MapControllers());
like image 25
alsami Avatar answered Sep 22 '22 08:09

alsami