[英]Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing
I had an Asp.Net core 2.2 project.我有一个 Asp.Net 核心 2.2 项目。
Recently, I changed the version from .net core 2.2 to .net core 3.0 Preview 8. After this change I see this warning message:最近,我将版本从 .net core 2.2 更改为 .net core 3.0 Preview 8。在此更改后,我看到此警告消息:
using 'UseMvc' to configure MVC is not supported while using Endpoint Routing.使用端点路由时不支持使用“UseMvc”来配置 MVC。 To continue using 'UseMvc', please set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices'.要继续使用“UseMvc”,请在“ConfigureServices”中设置“MvcOptions.EnableEndpointRouting = false”。
I understand that by setting EnableEndpointRouting
to false I can solve the issue, but I need to know what is the proper way to solve it and why Endpoint Routing does not need UseMvc()
function.我知道通过将EnableEndpointRouting
设置为 false 我可以解决这个问题,但我需要知道什么是解决它的正确方法以及为什么 Endpoint Routing 不需要UseMvc()
函数。
I found the solution, in the following official documentation " Migrate from ASP.NET Core 2.2 to 3.0 ":我在以下官方文档“ Migrate from ASP.NET Core 2.2 to 3.0 ”中找到了解决方案:
There are 3 approaches:有3种方法:
- Replace UseMvc or UseSignalR with UseEndpoints.用 UseEndpoints 替换 UseMvc 或 UseSignalR。
In my case, the result looked like that就我而言,结果看起来像这样
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() 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();
});
}
}
OR或者
3. Disable endpoint Routing. 3. 禁用端点路由。 As the exception message suggests and as mentioned in the following section of documentation: use mvcwithout endpoint routing正如异常消息所暗示的那样,并在文档的以下部分中提到: 使用 mvcwithout endpoint routing
services.AddMvc(options => options.EnableEndpointRouting = false);
//OR
services.AddControllers(options => options.EnableEndpointRouting = false);
This worked for me (add in Startup.cs
> ConfigureServices method):这对我Startup.cs
(在Startup.cs
> ConfigureServices 方法中添加):
services.AddMvc(option => option.EnableEndpointRouting = false)
but I need to know what is the proper way to solve it但我需要知道解决它的正确方法是什么
In general, you should use EnableEndpointRouting
instead of UseMvc
, and you could refer Update routing startup code for detail steps to enable EnableEndpointRouting
.通常,您应该使用EnableEndpointRouting
而不是UseMvc
,您可以参考更新路由启动代码了解启用EnableEndpointRouting
详细步骤。
why Endpoint Routing does not need UseMvc() function.为什么端点路由不需要 UseMvc() 函数。
For UseMvc
, it uses the IRouter-based logic
and EnableEndpointRouting
uses endpoint-based logic
.对于UseMvc
,它使用the IRouter-based logic
而EnableEndpointRouting
使用endpoint-based logic
。 They are following different logic which could be found below:他们遵循不同的逻辑,可以在下面找到:
if (options.Value.EnableEndpointRouting)
{
var mvcEndpointDataSource = app.ApplicationServices
.GetRequiredService<IEnumerable<EndpointDataSource>>()
.OfType<MvcEndpointDataSource>()
.First();
var parameterPolicyFactory = app.ApplicationServices
.GetRequiredService<ParameterPolicyFactory>();
var endpointRouteBuilder = new EndpointRouteBuilder(app);
configureRoutes(endpointRouteBuilder);
foreach (var router in endpointRouteBuilder.Routes)
{
// Only accept Microsoft.AspNetCore.Routing.Route when converting to endpoint
// Sub-types could have additional customization that we can't knowingly convert
if (router is Route route && router.GetType() == typeof(Route))
{
var endpointInfo = new MvcEndpointInfo(
route.Name,
route.RouteTemplate,
route.Defaults,
route.Constraints.ToDictionary(kvp => kvp.Key, kvp => (object)kvp.Value),
route.DataTokens,
parameterPolicyFactory);
mvcEndpointDataSource.ConventionalEndpointInfos.Add(endpointInfo);
}
else
{
throw new InvalidOperationException($"Cannot use '{router.GetType().FullName}' with Endpoint Routing.");
}
}
if (!app.Properties.TryGetValue(EndpointRoutingRegisteredKey, out _))
{
// Matching middleware has not been registered yet
// For back-compat register middleware so an endpoint is matched and then immediately used
app.UseEndpointRouting();
}
return app.UseEndpoint();
}
else
{
var routes = new RouteBuilder(app)
{
DefaultHandler = app.ApplicationServices.GetRequiredService<MvcRouteHandler>(),
};
configureRoutes(routes);
routes.Routes.Insert(0, AttributeRouting.CreateAttributeMegaRoute(app.ApplicationServices));
return app.UseRouter(routes.Build());
}
For EnableEndpointRouting
, it uses EndpointMiddleware to route the request to the endpoints.对于EnableEndpointRouting
,它使用EndpointMiddleware将请求路由到端点。
The issue I found to be due to updates on the .NET Core framework.我发现的问题是由于 .NET Core 框架的更新造成的。 The latest .NET Core 3.0 released version requires explicit opt-in for using MVC.最新的 .NET Core 3.0 发布版本需要显式选择加入以使用 MVC。
This issue is most visible when one tries to migrate from older .NET Core(2.2 or preview 3.0 version) to .NET Core 3.0当尝试从较旧的 .NET Core(2.2 或预览版 3.0 版本)迁移到 .NET Core 3.0 时,此问题最为明显
If migrating from 2.2 to 3.0, please use the below code to fix the issue.如果从 2.2 迁移到 3.0,请使用以下代码解决问题。
services.AddMvc(options => options.EnableEndpointRouting = false);
If using .NET Core 3.0 template,如果使用 .NET Core 3.0 模板,
services.AddControllers(options => options.EnableEndpointRouting = false);
ConfigServices method after fix as below,修复后的 ConfigServices 方法如下,
Thank You谢谢你
You can use : in ConfigureServices method:您可以在 ConfigureServices 方法中使用:
services.AddControllersWithViews();
And for Configure method:对于配置方法:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Endpoint Routing is disabled by default on ASP.NET 5.0 ASP.NET 5.0 上默认禁用端点路由
Just configure as in Startup只需像启动时一样配置
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => options.EnableEndpointRouting = false);
}
This worked for me这对我有用
For DotNet Core 3.1对于 DotNet 核心 3.1
Use below下面使用
File : Startup.cs public void Configure(IApplicationBuilder app, IHostingEnvironment env) {文件: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();
});
}
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}");
});
}
}
This works also in .Net Core 5这也适用于 .Net Core 5
This worked for me这对我有用
services.AddMvc(options => options.EnableEndpointRouting = false); or
OR
services.AddControllers(options => options.EnableEndpointRouting = false);
-> In ConfigureServices method - Startup.cs -> 在 ConfigureServices 方法中 - Startup.cs
//*****REGISTER Routing Service*****
services.AddMvc();
services.AddControllers(options => options.EnableEndpointRouting = false);
-> In Configure Method - Startup.cs -> 在配置方法中 - Startup.cs
//*****USE Routing*****
app.UseMvc(Route =>{
Route.MapRoute(
name:"default",
template: "{Controller=Name}/{action=Name}/{id?}"
);
});
I had an Asp.Net core 2.2 project.我有一个Asp.Net core 2.2项目。
Recently, I changed the version from .net core 2.2 to .net core 3.0 Preview 8. After this change I see this warning message:最近,我将版本从.net core 2.2更改为.net core 3.0 Preview8。更改之后,我看到以下警告消息:
using 'UseMvc' to configure MVC is not supported while using Endpoint Routing.使用端点路由时,不支持使用“ UseMvc”配置MVC。 To continue using 'UseMvc', please set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices'.要继续使用“ UseMvc”,请在“ ConfigureServices”中设置“ MvcOptions.EnableEndpointRouting = false”。
I understand that by setting EnableEndpointRouting
to false I can solve the issue, but I need to know what is the proper way to solve it and why Endpoint Routing does not need UseMvc()
function.我知道通过将EnableEndpointRouting
设置为false可以解决此问题,但是我需要知道什么是解决该问题的正确方法,以及为什么Endpoint Routing不需要UseMvc()
函数。
Use Below Code使用下面的代码
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.