首页 > 解决方案 > 尝试运行使用来自本地 IIS 的 swagger 的 .net core api

问题描述

我创建了一个 .Net 核心 Web api 应用程序并使用 swagger 。我正在尝试为应用程序创建一个配置文件,以便在从 Visual Studio 2019 运行应用程序时在 IIS 上运行它。

应用程序文件夹具有完全权限,应用程序池/网站都在具有完全管理员权限的服务帐户下运行。

我正在尝试将应用程序配置为使用 https 在特定端口 443 上运行。不确定我是否必须进行任何代码更改以便应用程序在特定端口上运行

如果我尝试使用带有 url https://localhost:portnumber/TestApi/swagger的 IIS express 运行应用程序。它工作得很好

如果我尝试使用为 IIS 配置的配置文件来运行带有 URL“ https://localhost/TestApi/swagger的应用程序,那么它会抛出错误。

下面是我的 web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="bin\IISSupport\VSIISExeLauncher.exe" arguments="-argFile IISExeLauncherArgs.txt" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_HTTPS_PORT" value="44342" />
        <environmentVariable name="COMPLUS_ForceENC" value="1" />
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>

我的 start up.cs 文件如下

 public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            //Configuration = configuration;
            var builder = new ConfigurationBuilder()
              .SetBasePath(env.ContentRootPath)
              .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
              .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true)
              .AddEnvironmentVariables();
            Configuration = builder.Build();
            ConfigSettingLayoutRenderer.DefaultConfiguration = Configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IConfiguration>(Configuration);
            services.Configure<IISOptions>(options =>
            {
                options.AutomaticAuthentication = false;
            });
            services.AddSwaggerGen(c => c.SwaggerDoc("v1",
                new Swashbuckle.AspNetCore.Swagger.Info()
                {
                    Title = "Test APIs",
                    Description = "REST APIs",
                    Version = "v1"
                })
            );

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
        {
            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.UseHttpsRedirection();
            app.UseMvc();
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                string swaggerJsonBasePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
                c.SwaggerEndpoint($"{swaggerJsonBasePath}/swagger/v1/swagger.json", "My API");
            });
            app.Run(async (context) =>
            {
                context.Response.Redirect("TestApi/swagger");
                await Task.FromResult(0);
            });
        }
    }

和我的launchsettings.json 如下

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iis": {
      "applicationUrl": "https://localhost/TestApi",
      "sslPort": 0
    },
    "iisExpress": {
      "applicationUrl": "http://localhost:57086/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "PmDashBoard.Api": {
      "commandName": "IIS",
      "launchBrowser": true,
      "launchUrl": "https://localhost/TestApi",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost/TestApi"
    }
  }
}

我的机器上安装了 .Net 核心托管包,并安装了最新版本的 VS2019。我正在尝试 Ctrl + F5 从 VS2019 运行应用程序并收到以下错误

HTTP 错误 500.19 - 内部服务器错误

无法访问请求的页面,因为该页面的相关配置数据无效。

详细错误信息:

模块 IIS Web 核心

通知开始请求

处理程序尚未确定

错误代码 0x80070003

配置错误 无法读取配置文件

标签: iis.net-coreswagger

解决方案


推荐阅读