首页 > 解决方案 > http://localhost:xxxx/api/values 无法在 .net core 2.2 中加载页面

问题描述

我正在使用 .net core 2.2 使用 WebApi。如果我使用 URL https://localhost:44352/api/values访问 API ,它可以正常工作。但是,如果我改为使用http://localhost:44352/api/values,使用 HTTP 而不是 HTTPS,则无法加载。

这是 Startup.cs 中的配置

   public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        //app.UseSwagger();
        //app.UseSwaggerUI(x =>
        //{
        //    x.SwaggerEndpoint(@"/swagger/v1/swagger.json", "My Api v1");
        //    x.RoutePrefix = "api";

        //});

        app.UseHttpsRedirection();
        app.UseMvc();
    }

谁能向我解释什么是错的?

标签: .nethttpasp.net-web-apiasp.net-corehttps

解决方案


由于app.UseHttpsRedirection();,您将管道设置为将每个 HTTP 调用重定向到 HTTPS。

所以首先,删除这条线。

然后,删除app.UseHsts();. 如果您不想使用 HTTPS,则无需强制使用 SSL。

现在,您应该能够使用 HTTP 调用来调用您的 API。


推荐阅读