問題描述:剛配置好了swagger,沿用netcore2.2的配置繼續在瀏覽器打開api控制器,結果報錯了。。。
解決方法:從路由層面查找了半天原因,無果,最后把api頁面中的跨域設置去掉后正常了,看來和跨越設置有關,繼續查找,原來是注冊時候順序導致的問題
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseStaticFiles(); app.UseCookiePolicy(); app.UseRouting(); //允許跨域 位置和必須放對,否則加載出錯 app.UseCors("any"); app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); //啟用中間件服務生成Swagger作為JSON終結點 app.UseSwagger(); //啟用中間件服務對swagger-ui,指定Swagger JSON終結點 app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); }); var log = LogManager.GetLogger(repository.Name, typeof(Startup)); log.Info("Start Runing"); }
跨域設置加載必須放到UseRouting和UseEndpoints中間