Home > front end >  InvalidOperationException: EndpointRoutingMiddleware matches endpoints setup by EndpointMiddleware a
InvalidOperationException: EndpointRoutingMiddleware matches endpoints setup by EndpointMiddleware a

Time:09-16

I have a .Net 5.0 API project that stopped working after some recent changes. As there were many changes I still haven't found the reason for the problem that is causing this error:

"InvalidOperationException: 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'"

Startup.cs Basic Code:

public void ConfigureServices(IServiceCollection services)
    {

        services.AddControllers();            
        services.AddAutoMapper(typeof(Startup));

        var mapperConfig = new MapperConfiguration(mc =>
        {
            mc.AddProfile(new MappingProfile());
        });

        IMapper mapper = mapperConfig.CreateMapper();
                  

    }

  
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseExceptionHandler("/error");
        

        app.UseAuthorization();
        app.UseAuthentication();

        app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());

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

CodePudding user response:

There is a missing app.UseRouting(); line in the Configure method. Put it before app.UseAuthorization(); line, as per guidelines.

  • Related