且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

方法 'UseRouting' 没有重载需要 1 个参数

更新时间:2023-02-14 17:49:19

再次引用错误信息:

EndpointRoutingMiddleware 匹配 EndpointMiddleware 设置的端点,因此必须在 EndpointMiddleware 之前添加到请求执行管道中.请通过在应用程序启动代码中对Configure(...)"的调用中调用IApplicationBuilder.UseRouting"来添加EndpointRoutingMiddleware.

EndpointRoutingMiddleware matches endpoints setup by EndpointMiddleware and so must be added to the request execution pipeline before EndpointMiddleware. Please add EndpointRoutingMiddleware by calling 'IApplicationBuilder.UseRouting' inside the call to 'Configure(...)' in the application startup code.

ASP.NET Core 3 使用改进的端点路由,这通常会在应用程序中提供更多关于路由的控制.端点路由分为两个独立的步骤:

ASP.NET Core 3 uses a refined endpoint routing which will generally give more control about routing within the application. Endpoint routing works in two separate steps:

  • 第一步,将请求的路由与配置的路由进行匹配,以确定正在访问的路由.
  • 在最后一步中,正在评估确定的路由以及相应的中间件,例如MVC,被调用.

这是允许其他中间件在这些点之间起作用的两个独立步骤.这允许这些中间件利用来自端点路由的信息,例如处理授权,而不必作为实际 handler(例如 MVC)的一部分执行.

These are two separate steps to allow other middlewares to act between those points. That allows those middlewares to utilize the information from endpoint routing, e.g. to handle authorization, without having to execute as part of the actual handler (e.g. MVC).

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

The two steps are set up by app.UseRouting() and app.UseEndpoints(). The former will register the middleware that runs the logic to determine the route. The latter will then execute that route.

如果你仔细阅读错误信息,在 EndpointRoutingMiddlewareEndpointMiddleware 的用法有点混乱,它会告诉你添加 UseRouting() Configure 方法的内部.所以基本上,你忘了调用端点路由的第一步.

If you read the error message carefully, between the somewhat confusing usage of EndpointRoutingMiddleware and EndpointMiddleware, it will tell you to add UseRouting() inside of the Configure method. So basically, you forgot to invoke the first step of endpoint routing.

所以您的 Configure 应该(例如)如下所示:

So your Configure should (for example) look like this:

app.UseRouting();

app.UseAuthentication();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
});

到 3.0 的路由迁移也记录在 3.0 迁移指南.

The routing migration to 3.0 is also documented in the migration guide for 3.0.