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

时间:2019-05-15 19:50:23

标签: c# asp.net .net asp.net-mvc asp.net-core

我刚刚更新到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通过以下方式匹配设置的端点   EndpointMiddleware等必须添加到请求执行中   EndpointMiddleware之前的管道。请加   通过调用“ IApplicationBuilder.UseRouting”来访问EndpointRoutingMiddleware   在应用程序启动代码中对“ Configure(...)”的调用中。”

我尝试替换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

2 个答案:

答案 0 :(得分:2)

再次引用错误消息:

  

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

ASP.NET Core 3使用完善的端点路由,它通常会为应用程序内的路由提供更多控制。端点路由分为两个独立的步骤:

  • 第一步,再对请求的路由进行匹配,然后再对配置的路由进行匹配,以找出正在访问的路由。
  • 在最后的步骤中,对确定的路由进行评估,并使用相应的中间件,例如称为MVC。

这是两个单独的步骤,以允许其他中间件在这些点之间起作用。这允许那些中间件利用来自端点路由的信息,例如来处理授权,而不必执行为实际的 handler (例如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();
});

migration guide for 3.0中也记录了向3.0的路由迁移。

答案 1 :(得分:0)

经过一番挖掘,我找到了解决该问题的方法。由于dotnet core 3.0需要一些额外的操作,因此我将说明为完成这项工作所做的工作。首先在ConfigureServices()方法(在Startup.cs中)中删除:

services.AddMvc().AddNewtonsoftJson();

在此方法的顶部(在services.Configure <>下)添加以下行:

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

下一步,在Configure()方法中,在app.UseRouting()app.UseAuthentication()之前添加app.UseAuthorization();,并在此方法的底部替换

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

宽度:

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