app.UseMvcWithDefaultRoute();添加后会报错为啥呢

来源:3-2 Demo 添加路由

伊人爱红妆

2020-03-27 20:35

app.UseMvcWithDefaultRoute();添加后会提示System.InvalidOperationException:“Endpoint Routing does not support 'IApplicationBuilder.UseMvc(...)'. To use 'IApplicationBuilder.UseMvc' set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices(...).”

写回答 关注

6回答

  • 阿莱克斯刘
    2020-04-08 06:14:30

    是不是使用的是net core 3+?课程是基于2.2的,所以会跟3+有所不同。试试: "services.AddMvc(option => option.EnableEndpointRouting = false) .AddNewtonsoftJson();", 意思就是默认禁用Endpoint Routing

  • 阿莱克斯刘
    2020-04-01 06:52:31

    感觉像是依赖没添加,截图留下startup文件代码,我可以仔细看看。

  • 米兰的小屋
    2021-04-22 14:04:24

    要把

    endpoints.MapGet("/", async context =>

                    {

                        await context.Response.WriteAsync("Hello World!");

                    });

    这句话删掉,否则就会覆盖HomeController里的设置

  • weixin_慕前端5562310
    2020-05-04 22:42:43

     app.UseEndpoints(endpoints =>

                {

                    endpoints.MapGet("/", async context =>

                    {

                        await context.Response.WriteAsync("Hello World!");

                    });


                    endpoints.MapDefaultControllerRoute();

                });

    3.1版本,这么写就OK啦,很简单!

  • weixin_慕前端5562310
    2020-05-04 22:36:13

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

            {

                if (env.IsDevelopment())

                {

                    app.UseDeveloperExceptionPage();

                }


                app.UseRouting();


                app.UseEndpoints(endpoints =>

                {

                    endpoints.MapGet("/", async context =>

                    {

                        await context.Response.WriteAsync("Hello World!");

                    });


                    endpoints.MapControllerRoute(

                        name: "default",

                        pattern: "{controller=Home}/{action=Index}/{id?}");

                });

            }

    这是asp.net core 3.1的添加方法,用这个就行了

  • 伊人爱红妆
    2020-04-01 19:05:47

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;

    namespace CityGarden
    {
        public class Startup
        {
            // This method gets called by the runtime. Use this method to add services to the container.
            // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc();
            }

            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            //请求通道都是通过IApplicationBuilder创建的。
            //每个中间件都可以截获、传递、修改请求对象,输出响应对象。
            //特定情况下某些中间件可以做短路处理,直接像前端输出响应对象。
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                //自定义路由

                /*app.UseMvc(route =>
                {
                    route.MapRoute("default","{controller=Home}/{action=index}/{id?}");
                });*/


                app.Map("/test", build =>
                {
                    build.Run(async context =>
                    {
                        await context.Response.WriteAsync("Hello form test!");
                    });
                });

            }
        }
    }


打造你的第一个ASP.NET5 MVC网站应用

熟练掌握ASP.Net 5 MVC的开发原则与使用方式

19277 学习 · 194 问题

查看课程

相似问题