netcore3.1配置webapi Controller打开报错

偶尔善良 提交于 2020-03-01 07:53:46

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!