为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

正在回答

6 回答

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

2 回复 有任何疑惑可以回复我~

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

0 回复 有任何疑惑可以回复我~

要把

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

                {

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

                });

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

0 回复 有任何疑惑可以回复我~

 app.UseEndpoints(endpoints =>

            {

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

                {

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

                });


                endpoints.MapDefaultControllerRoute();

            });

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

0 回复 有任何疑惑可以回复我~

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的添加方法,用这个就行了

1 回复 有任何疑惑可以回复我~

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!");
                });
            });

        }
    }
}


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信