gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:29:32 26 4
gpt4 key购买 nike

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

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

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

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

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

'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 方法中的以下行:

    services.AddMvc()
.AddNewtonsoftJson();

宽度:

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

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

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

 <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

最佳答案

再次引用错误信息:

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 使用改进的端点路由,这通常会提供对应用程序内路由的更多控制。端点路由分为两个独立的步骤:

  • 第一步,将请求的路由与配置的路由再次匹配,以确定正在访问的路由。
  • 在最后一步中,正在评估确定的路由和相应的中间件,例如MVC,被称为。

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

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

如果您仔细阅读错误消息,在 EndpointRoutingMiddlewareEndpointMiddleware 之间有些困惑的用法之间,它会告诉您添加 UseRouting()Configure 方法中。所以基本上,您忘记了调用端点路由的第一步。

所以你的Configure应该(例如)看起来像这样:

app.UseRouting();

app.UseAuthentication();

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

到 3.0 的路由迁移也记录在 migration guide for 3.0 中.

关于c# - 方法 'UseRouting' 没有重载需要 1 个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56156657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com