app.UseMvcWithDefaultRoute();添加后會報(bào)錯(cuò)為啥呢
app.UseMvcWithDefaultRoute();添加后會提示System.InvalidOperationException:“Endpoint Routing does not support 'IApplicationBuilder.UseMvc(...)'. To use 'IApplicationBuilder.UseMvc' set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices(...).”
2020-04-08
是不是使用的是net core 3+?課程是基于2.2的,所以會跟3+有所不同。試試: "services.AddMvc(option => option.EnableEndpointRouting = false) .AddNewtonsoftJson();", 意思就是默認(rèn)禁用Endpoint Routing
2020-04-01
感覺像是依賴沒添加,截圖留下startup文件代碼,我可以仔細(xì)看看。
2021-04-22
要把
endpoints.MapGet("/", async context =>
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? await context.Response.WriteAsync("Hello World!");
? ? ? ? ? ? ? ? });
這句話刪掉,否則就會覆蓋HomeController里的設(shè)置
2020-05-04
?app.UseEndpoints(endpoints =>
? ? ? ? ? ? {
? ? ? ? ? ? ? ? endpoints.MapGet("/", async context =>
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? await context.Response.WriteAsync("Hello World!");
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? endpoints.MapDefaultControllerRoute();
? ? ? ? ? ? });
3.1版本,這么寫就OK啦,很簡單!
2020-05-04
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的添加方法,用這個(gè)就行了
2020-04-01
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創(chuàng)建的。
??????? //每個(gè)中間件都可以截獲、傳遞、修改請求對象,輸出響應(yīng)對象。
??????? //特定情況下某些中間件可以做短路處理,直接像前端輸出響應(yīng)對象。
??????? 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!");
??????????????? });
??????????? });
??????? }
??? }
}