简体   繁体   English

使用 app.UseMvc() 或 app.UseEndpoints 在 asp.net core mvc 项目中设置路由有什么区别?

[英]What is difference between setting up routing in asp.net core mvc project with app.UseMvc() or app.UseEndpoints?

I know that when migrating from asp.net 2.2 to 3 app.UseMvc() line in startup.cs file's configure method is replaced by app.UseRouting();我知道当从 asp.net 2.2 迁移到 3 app.UseMvc() 行时,startup.cs 文件的配置方法被 app.UseRouting(); 替换; app.UseAuthorization(); app.UseAuthorization(); app.UseEndpoints();. app.UseEndpoints();。

Can anyone explain how app.UseEndpoints() and app.UseMvc() works internally ?谁能解释一下 app.UseEndpoints() 和 app.UseMvc() 是如何在内部工作的?

Under the asp.net core mvc 2.2 framework, to use traditional routing, you must configure the IRouteBuilder interface in the UseMVC middleware.在asp.net core mvc 2.2框架下,要使用传统路由,必须在UseMVC中间件中配置IRouteBuilder接口。

In the Configure method of the application Startup, the default routing settings are as follows:在应用Startup的Configure方法中,默认路由设置如下:

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

In the call to UseMvc, MapRoute is used to create a single route, also known as the default route.在对 UseMvc 的调用中,MapRoute 用于创建单个路由,也称为默认路由。 Most MVC applications use routing with templates.大多数 MVC 应用程序使用带有模板的路由。 A convenient method for the default route can be used:可以使用默认路由的便捷方法:

app.UseMvcWithDefaultRoute();

UseMvc and UseMvcWithDefaultRoute can add an instance of RouterMiddleware to the middleware pipeline. UseMvc 和 UseMvcWithDefaultRoute 可以将 RouterMiddleware 的实例添加到中间件管道中。 MVC does not directly interact with middleware, but uses routing to process requests. MVC 不直接与中间件交互,而是使用路由来处理请求。 MVC connects to the route through an instance of MvcRouteHandler. MVC 通过 MvcRouteHandler 的实例连接到路由。

UseMvc does not directly define any routes. UseMvc 不直接定义任何路由。 It adds placeholders {controller=Home}/{action=Index}/{id?} to the route collection of attribute routing.它在属性路由的路由集合中添加了占位符{controller=Home}/{action=Index}/{id?}。 By overloading UseMvc(Action), users are allowed to add their own routes, and attribute routing is also supported.通过重载UseMvc(Action),允许用户添加自己的路由,也支持属性路由。

UseEndpoints : Perform matching endpoints. UseEndpoints :执行匹配的端点。

It separates routing matching and resolution functions from endpoint execution functions, and until now, these functions are bundled with MVC middleware.它将路由匹配和解析功能与端点执行功能分开,直到现在,这些功能都与 MVC 中间件捆绑在一起。

First of all,you could have a look at their 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 uses a refined endpoint routing which will generally give more control about routing within the application. ASP.NET Core 3 使用改进的端点路由,这通常会对应用程序内的路由提供更多控制。 Endpoint routing works in two separate steps:端点路由分为两个独立的步骤:

In a first step, the requested route is matched agains the configured routes to figure out what route is being accessed.在第一步中,请求的路由与配置的路由再次匹配,以确定正在访问的路由。

In a final step, the determined route is being evaluated and the respective middleware, eg MVC, is called.在最后一步中,正在评估确定的路由并调用相应的中间件,例如 MVC。

The two steps are set up by app.UseRouting() and app.UseEndpoints().这两个步骤由 app.UseRouting() 和 app.UseEndpoints() 设置。 The former will register the middleware that runs the logic to determine the route.前者将注册运行逻辑以确定路由的中间件。 The latter will then execute that route.然后后者将执行该路线。

Also, refer to:另外,请参考:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net 中的 app.UseMvc 路由不存在 - app.UseMvc routes in asp.net doesn't exists ASP.NET Core 1和Entity Framework(Npgsql)中断了启动类,并说:app.UseMvc方法不可用 - ASP.NET Core 1 and Entity Framework (Npgsql) breaks Startup class and says: app.UseMvc method not available ASP.NET CORE 没有 app.UseEndpoints() 方法 - ASP.NET CORE Don't have app.UseEndpoints() Method app.Map 和 app.UseEndpoints + endpoints.Map 的区别? - Difference between app.Map and app.UseEndpoints + endpoints.Map? 为什么我必须在ASP 5中的app.UseMvc之前调用app.UseErrorHandler才能使用它? - Why do I have to call app.UseErrorHandler before app.UseMvc in ASP 5 for it to work? ASP.NET MVC 5、ASP.NET Core MVC 5 有什么区别? - What is the difference between ASP.NET MVC 5, ASP.NET Core MVC 5? 在ASP.NET Core Web App中,AddJsonOptions和AddJsonFormatters之间的区别是什么? - In an ASP.NET Core Web App, what's the difference between AddJsonOptions and AddJsonFormatters? ASP.NET Web App和ASP.NET MVC 3 Empty Web App之间的区别? - Difference between ASP.NET Web App and ASP.NET MVC 3 Empty Web App? 创建项目 ASP.NET Core (.NET Core) 和 ASP.NET Core (.NET Framework) 有什么区别 - What is the difference between creating a project ASP.NET Core (.NET Core) and ASP.NET Core (.NET Framework) ASP.NET MVC中<%:和<%=有什么区别? - What is the difference between <%: and <%= in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM