使用端点路由时不支持使用“UseMvc"配置 MVC [英] Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing
问题描述
我有一个 Asp.Net 核心 2.2 项目.
I had an Asp.Net core 2.2 project.
最近,我将版本从 .net core 2.2 更改为 .net core 3.0 Preview 8.此更改后,我看到此警告消息:
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:
使用 Endpoint 时不支持使用UseMvc"来配置 MVC路由.要继续使用UseMvc",请设置'ConfigureServices' 中的 'MvcOptions.EnableEndpointRouting = false'.
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()
函数.
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.
推荐答案
但我需要知道解决它的正确方法是什么
but I need to know what is the proper way to solve it
通常,您应该使用 EnableEndpointRouting
而不是 UseMvc
,您可以参考 更新路由启动代码了解详情启用 EnableEndpointRouting
的步骤.
In general, you should use EnableEndpointRouting
instead of UseMvc
, and you could refer Update routing startup code for detail steps to enable EnableEndpointRouting
.
为什么端点路由不需要 UseMvc() 函数.
why Endpoint Routing does not need UseMvc() function.
对于UseMvc
,它使用基于IRouter的逻辑
,EnableEndpointRouting
使用基于端点的逻辑
.他们遵循不同的逻辑,可以在下面找到:
For UseMvc
, it uses the IRouter-based logic
and EnableEndpointRouting
uses 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());
}
对于EnableEndpointRouting
,它使用EndpointMiddleware 将请求路由到端点.
For EnableEndpointRouting
, it uses EndpointMiddleware to route the request to the endpoints.
这篇关于使用端点路由时不支持使用“UseMvc"配置 MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!