在使用 Endpoint Routing 时,不支持使用“UseMvc”来配置 MVC。

31 浏览
0 Comments

在使用 Endpoint Routing 时,不支持使用“UseMvc”来配置 MVC。

我有一个Asp.Net core 2.2项目。

最近,我将版本从.net core 2.2更改为.net core 3.0 Preview 8。在这个变化之后,我看到了这个警告信息:

当使用Endpoint Routing时,使用“ UseMvc”配置MVC不被支持。要继续使用“ UseMvc”,请在“ ConfigureServices”中设置“ MvcOptions.EnableEndpointRouting = false”。

我知道通过将EnableEndpointRouting设置为false可以解决此问题,但我需要知道正确的解决方法以及为什么Endpoint Routing不需要UseMvc()函数。

admin 更改状态以发布 2023年5月20日
0
0 Comments

对我有效(在Startup.cs > ConfigureServices方法中添加):

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

我在以下官方文档 "ASP.NET Core 2.2升级到3.0"中发现了解决方案:

有3种方式:

  1. 使用UseEndpoints替换UseMvc或UseSignalR。

在我的情况中,结果看起来像这样:

  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}");
        });
    }
}


2.使用AddControllers()和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();
        });
    }
}


3.禁用终点路由。根据异常消息提示和文档的以下部分所述:不使用终点路由的mvc

services.AddMvc(options => options.EnableEndpointRouting = false);
//OR
services.AddControllers(options => options.EnableEndpointRouting = false);

0