【问题标题】:What is difference between setting up routing in asp.net core mvc project with app.UseMvc() or app.UseEndpoints?使用 app.UseMvc() 或 app.UseEndpoints 在 asp.net core mvc 项目中设置路由有什么区别?
【发布时间】:2021-12-17 22:10:43
【问题描述】:

我知道当从 asp.net 2.2 迁移到 3 app.UseMvc() 行时,startup.cs 文件的配置方法被 app.UseRouting(); app.UseAuthorization(); app.UseEndpoints();.

谁能解释一下 app.UseEndpoints() 和 app.UseMvc() 在内部是如何工作的?

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-core asp.net-mvc-3 asp.net-mvc-routing


    【解决方案1】:

    在asp.net core mvc 2.2框架下,要使用传统路由,必须在UseMVC中间件中配置IRouteBuilder接口。

    在应用Startup的Configure方法中,默认路由设置如下:

    app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
    {
    

    在调用 UseMvc 时,MapRoute 用于创建单个路由,也称为默认路由。大多数 MVC 应用程序使用带有模板的路由。可以使用默认路由的便捷方法:

    app.UseMvcWithDefaultRoute();
    

    UseMvc 和 UseMvcWithDefaultRoute 可以将 RouterMiddleware 的实例添加到中间件管道中。 MVC 不直接与中间件交互,而是使用路由来处理请求。 MVC 通过 MvcRouteHandler 的实例连接路由。

        UseMvc 没有直接定义任何路由。它将占位符 {controller=Home}/{action=Index}/{id?} 添加到属性路由的路由集合中。通过重载UseMvc(Action),允许用户添加自己的路由,也支持属性路由。

    UseEndpoints:执行匹配端点。

    它将路由匹配和解析功能与端点执行功能分开,直到现在,这些功能都与 MVC 中间件捆绑在一起。

    首先,你可以看看他们的source code

    public static IApplicationBuilder UseEndpoints(this IApplicationBuilder builder, Action<IEndpointRouteBuilder> configure)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
    
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }
    
            VerifyRoutingServicesAreRegistered(builder);
    
            VerifyEndpointRoutingMiddlewareIsRegistered(builder, out var endpointRouteBuilder);
    
            configure(endpointRouteBuilder);
    
            // Yes, this mutates an IOptions. We're registering data sources in a global collection which
            // can be used for discovery of endpoints or URL generation.
            //
            // Each middleware gets its own collection of data sources, and all of those data sources also
            // get added to a global collection.
            var routeOptions = builder.ApplicationServices.GetRequiredService<IOptions<RouteOptions>>();
            foreach (var dataSource in endpointRouteBuilder.DataSources)
            {
                routeOptions.Value.EndpointDataSources.Add(dataSource);
            }
    
            return builder.UseMiddleware<EndpointMiddleware>();
        }
    

    ASP.NET Core 3 使用改进的端点路由,通常可以更好地控制应用程序内的路由。端点路由分为两个独立的步骤:

    第一步,将请求的路由与配置的路由进行匹配,以确定正在访问的路由。

    在最后一步中,正在评估确定的路由和相应的中间件,例如MVC,被调用。

    这两个步骤由 app.UseRouting() 和 app.UseEndpoints() 设置。前者将注册运行逻辑以确定路由的中间件。然后后者将执行该路由。

    另外,参考:

    https://asp.net-hacker.rocks/2019/08/12/aspnetcore30-look-into-startup.htmlhttps://aregcode.com/blog/2019/dotnetcore-understanding-aspnet-endpoint-routing/

    【讨论】:

      猜你喜欢
      • 2020-02-14
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2016-11-08
      相关资源
      最近更新 更多