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

No overload for method #39;UseRouting#39; takes 1 arguments(方法 UseRouting 没有重载需要 1 个参数)

本文介绍了方法 'UseRouting' 没有重载需要 1 个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚更新到 ASP.NET Core 3 Preview 5,现在当我打开我的解决方案并尝试构建它时会抛出错误在 Configure() 的 Startup.cs 文件中,方法 'UseRouting' 没有重载需要 1 个参数",代码如下:

I just updated to ASP.NET Core 3 Preview 5, now when I open my solution and try to build it throws the error 'No overload for method 'UseRouting' takes 1 arguments' in the Startup.cs file in the Configure() at the following code:

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

我确实阅读了有关 Microsoft 文档的一些文档,并尝试将上面的代码替换为:

I did read some documentation on the Microsoft docs and tried replacing the above code with:

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

但是,在构建期间会抛出具有以下上下文的 System.InvalidOperationException:

However, during build time that throws an System.InvalidOperationException with the following context:

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

'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.'

我尝试在 ConfigureServices 方法中替换以下行:

I tried replacing the following line in the ConfigureServices method:

    services.AddMvc()
        .AddNewtonsoftJson();

宽度:

    services.AddControllersWithViews()
        .AddNewtonsoftJson();
    services.AddRazorPages();

这不会再引发错误,但我的页面在完成加载时是空白的.谁能帮我解决这个问题?

This does not throw errors anymore but my page is blank when it finishes loading. Who can help me solve this issue?

对于我的解决方案,我使用以下软件包:

For my solution I use the following packages:

 <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0-preview5.19227.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0-preview5.19227.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0-preview5.19227.9" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />

我的解决方案的 TargetFramework 是 netcoreapp3.0

The TargetFramework of my solution is netcoreapp3.0

推荐答案

再次引用错误信息:

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.

这篇关于方法 'UseRouting' 没有重载需要 1 个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:方法 'UseRouting' 没有重载需要 1 个参数

基础教程推荐