使用端点路由时,不支持使用“ UseMvc”来配置MVC [英] Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing

查看:1133
本文介绍了使用端点路由时,不支持使用“ UseMvc”来配置MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I had an Asp.Net core 2.2 project.

最近,我将版本从.net core 2.2更改为.net core 3.0 Preview8。更改之后,我看到以下警告消息:

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:


。要继续使用'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可以解决此问题,但是我需要知道什么是解决问题的正确方法,以及为什么端点路由不需要 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆