netcore3.1配置webapi Controller打开报错

问题描述:刚配置好了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中间

参考链接:https://www.cnblogs.com/puzi0315/p/12197612.html

posted @ 2020-03-01 07:08  wangbg  阅读(1251)  评论(0编辑  收藏  举报