.net core 3,MVC,在使用端点路由时不支持使用'UseMvcWithDefaultRoute'配置MVC [英] .net core 3 , MVC , Using 'UseMvcWithDefaultRoute' to configure MVC is not supported while using Endpoint Routing
问题描述
我正在尝试创建一个基于ASP.NET Core 3的简单项目.
I am trying to create a simple project based on ASP.NET Core 3.
ASP.NET Core 2.2的MVC模板在启动类中包含以下行:
The MVC template for ASP.NET Core 2.2 has the following line inside the startup-class:
app.UseMvcWithDefaultRoute();
此行在ASP.NET Core 2.2和路由中均能正常工作,但是,在ASP.NET Core 3.0中,它不会编译并显示以下错误
This line works perfectly in ASP.NET Core 2.2 and routing works, however, in ASP.NET Core 3.0 it doesn't compile and displays the following error
在使用端点路由时,不支持使用'UseMvcWithDefaultRoutee'配置MVC.
Using 'UseMvcWithDefaultRoutee' to configure MVC is not supported while using Endpoint Routing.
问题是:如何在MVC应用程序的.net核心版本3中配置路由?"
The question is: "How to configure routing in .net core version 3 for MVC application?"
推荐答案
I found the solution, in the following official documentation "Migrate from ASP.NET Core 2.2 to 3.0":
有3种方法:
- 禁用端点路由.
(add in Startup.cs)
services.AddMvc(option => option.EnableEndpointRouting = false)
OR
- 用UseEndpoints替换UseMvc或UseSignalR.
就我而言,结果看起来像这样
In my case, the result looked like that
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
- 使用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();
});
}
}
这篇关于.net core 3,MVC,在使用端点路由时不支持使用'UseMvcWithDefaultRoute'配置MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!