gpt4 book ai didi

c# - .net core 3,MVC,使用端点路由时不支持使用 'UseMvcWithDefaultRoute' 配置 MVC

转载 作者:太空宇宙 更新时间:2023-11-03 22:30:14 25 4
gpt4 key购买 nike

我正在尝试创建一个基于 ASP.NET Core 3 的简单项目。

ASP.NET Core 2.2 的 MVC 模板在启动类中有以下行:

app.UseMvcWithDefaultRoute();

此行在 ASP.NET Core 2.2 中完美运行并且路由有效,但是,在 ASP.NET Core 3.0 中它无法编译并显示以下错误

Using 'UseMvcWithDefaultRoutee' to configure MVC is not supported while using Endpoint Routing.

问题是:“如何在 .net core 版本 3 中为 MVC 应用程序配置路由?”

最佳答案

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

共有 3 种方法:

  1. Disable endpoint Routing.
(add in Startup.cs)

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

OR

  1. Replace UseMvc or UseSignalR with UseEndpoints.

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

  public class Startup
{

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}


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

  1. 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();
});

}
}

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

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