问答详情
源自:3-2 Demo 添加路由

我用的也是2.2,出现异常!

http://img1.mukewang.com/5e927d0000011ceb09890308.jpg

System.InvalidOperationException

  HResult=0x80131509

  Message=Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddMvc' inside the call to 'ConfigureServices(...)' in the application startup code.

  Source=Microsoft.AspNetCore.Mvc.Core

  StackTrace:

   at Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.VerifyMvcIsRegistered(IApplicationBuilder app)

   at Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.UseMvc(IApplicationBuilder app, Action`1 configureRoutes)

   at Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.UseMvcWithDefaultRoute(IApplicationBuilder app)

   at Lanzhoulamian.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in C:\Users\Administrator\Desktop\Lanzhoulamian\Lanzhoulamian\Startup.cs:line 27


提问者:一位不愿透露姓名的崔先生 2020-04-12 10:30

个回答

  • 阿莱克斯刘
    2020-04-15 12:44:20

    ?很强的学习能力!

  • 慕九州3374948
    2020-05-17 12:47:05

    services.AddMvc(options => options.EnableEndpointRouting = false),添加这条确实可以解决该问题,谢谢!

  • 一位不愿透露姓名的崔先生
    2020-04-12 13:23:52

    通过查找百度找到问题了!

    在ASP.NET Core 2.2中,新增了一种路由,叫做Endpoint(终结点)路由。本文将以往的路由系统称为传统路由

    本文通过源码的方式介绍传统路由和Endpoint路由部分核心功能和实现方法,具体功能上的差异见官方文档

    在升级到ASP.NET Core 2.2后,会自动启用Endpoint路由

          public void ConfigureServices(IServiceCollection services)

            {

                //添加

                services.AddMvc(options => options.EnableEndpointRouting = false)

        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            }