gpt4 book ai didi

c# - 使用终结点路由时不支持使用 'UseMvc' 配置 MVC

转载 作者:太空狗 更新时间:2023-10-29 20:15:38 25 4
gpt4 key购买 nike

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

最近,我将版本从 .net core 2.2 更改为 .net core 3.0 Preview 8。此更改后我看到此警告消息:

using 'UseMvc' to configure MVC is not supported while using Endpoint Routing. To continue using 'UseMvc', please set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices'.

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

最佳答案

我在以下官方文档“Migrate from ASP.NET Core 2.2 to 3.0”中找到了解决方案:

有3种方法:

  1. Replace UseMvc or UseSignalR with UseEndpoints.

在我的例子中,结果是这样的

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

}
}

OR
2. Use AddControllers() and 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();
});

}
}

OR
3. Disable endpoint Routing. As the exception message suggests and as mentioned in the following section of documentation: use mvcwithout endpoint routing


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

关于c# - 使用终结点路由时不支持使用 'UseMvc' 配置 MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57684093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com