我有一只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()函数。


当前回答

—>在“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?}"
            );
        });

其他回答

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

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

—>在“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?}"
            );
        });

你可以使用: 在ConfigureServices方法中:

services.AddControllersWithViews();

配置方法:

app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });

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

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