我有一只Asp。Net core 2.2项目。

最近,我把。net core 2.2版本改成了。net core 3.0 Preview 8。在这个更改之后,我看到这个警告消息:

使用'UseMvc'配置MVC时,使用端点是不支持的 路由。要继续使用“UseMvc”,请设置 “MvcOptions。EnableEndpointRouting = false' inside 'ConfigureServices'。

我明白,通过设置EnableEndpointRouting为false,我可以解决这个问题,但我需要知道解决它的正确方法是什么,以及为什么端点路由不需要UseMvc()函数。


当前回答

端点路由在ASP上默认是禁用的。NET 5.0

只需在启动中配置

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

这对我很有效

其他回答

这对我来说是有效的(在Startup.cs > ConfigureServices方法中添加):

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

对于DotNet核心3.1

使用下面的

文件:Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{         

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
       
    app.UseHttpsRedirection();
    app.UseRouting();
    app.UseAuthentication();
    app.UseHttpsRedirection();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}
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}");
        });
    }
}

这也适用于。net Core 5

—>在“ConfigureServices”方法中

        //*****REGISTER Routing Service*****
        services.AddMvc();
        services.AddControllers(options => options.EnableEndpointRouting = false);

-配置方法中的>—Startup.cs

       //*****USE Routing***** 
        app.UseMvc(Route =>{
            Route.MapRoute(
                name:"default",
                template: "{Controller=Name}/{action=Name}/{id?}"
            );
        });

使用以下代码

app.UseEndpoints(endpoints =>
            {
                endpoints.MapDefaultControllerRoute();
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });