InvalidOperationException:EndpointRoutingMiddleware 与 EndpointMiddleware 设置的端点匹配,因此必须添加到请求执行中

And*_*ino 13 c# asp.net-core-5.0

我有一个 .Net 5.0 API 项目,在最近进行一些更改后停止工作。由于进行了很多更改,我仍然没有找到导致此错误的问题的原因:

“InvalidOperationException:EndpointRoutingMiddleware 与 EndpointMiddleware 设置的端点匹配,因此必须在 EndpointMiddleware 之前添加到请求执行管道中。请通过调用“IApplicationBuilder.UseRouting”添加 EndpointRoutingMiddleware”

Startup.cs基本代码:

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();
        });
    }
Run Code Online (Sandbox Code Playgroud)

小智 25

app.UseRouting();该方法中缺少一行Configure。app.UseAuthorization();根据指南,将其放在行之前。