伊人爱红妆
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(...).”
是不是使用的是net core 3+?课程是基于2.2的,所以会跟3+有所不同。试试: "services.AddMvc(option => option.EnableEndpointRouting = false) .AddNewtonsoftJson();", 意思就是默认禁用Endpoint Routing
感觉像是依赖没添加,截图留下startup文件代码,我可以仔细看看。
要把
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
这句话删掉,否则就会覆盖HomeController里的设置
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
endpoints.MapDefaultControllerRoute();
});
3.1版本,这么写就OK啦,很简单!
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的添加方法,用这个就行了
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网站应用
19277 学习 · 194 问题
相似问题