< 返回技术文档列表

.Net Core 增加 Swagger 本地启动可以,发布IIS不行

发布时间:2022-11-17 23:00:04⊙投诉举报
版本 .net core 2.1
Program.cs 增加 UseIISIntegration()
using Microsoft.AspNetCore;using Microsoft.AspNetCore.Hosting;namespace WebApi{    public class Program    {        public static void Main(string[] args)        {            CreateWebHostBuilder(args).Build().Run();        }         public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>            WebHost.CreateDefaultBuilder(args)            #if  DEBUG                .UseUrls(AppConfigurtaion.GetSecondSection("Api", "api").ToString())//给程序启动设置url地址                .UseKestrel().UseIISIntegration()//使用Kestrel内部服务器                .UseStartup<Startup>();            #elif RELEASE               .UseStartup<Startup>().UseKestrel();            #endif    }}
修改 lauchSettings.json 文件:
lauchSettings.json.png修改结果.png
{  "iisSettings": {    "windowsAuthentication": false,    "anonymousAuthentication": true,    "iisExpress": {      "applicationUrl": "http://localHost:56343",      "sslPort": 0    }  },  "$schema": "http://json.schemastore.org/launchsettings.json",  "profiles": {    "IIS Express": {      "commandName": "IISExpress",      "launchBrowser": true,      "launchUrl": "swagger",      "environmentVariables": {        "ASPNETCORE_ENVIRONMENT": "Development"      }    },    "WebApi": {      "commandName": "Project",      "launchBrowser": true,      "environmentVariables": {        "ASPNETCORE_URLS": "http://*:5000",        "ASPNETCORE_ENVIRONMENT": "Development"      },      "applicationUrl": "http://localhost:5000"    }  }}
Startup.cs
          services.AddSwaggerGen(c =>            {                c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info                {                    Title = "WebApi接口文档",                    Version = "v1.0.0"                });                var xmlPath = Path.Combine(AppContext.BaseDirectory, "WebApi.xml");                c.IncludeXmlComments(xmlPath,true);                //增加对控制器的标签(形容)                c.DocumentFilter<SwaggerDocTag>();                c.OperationFilter<AuthTokenHeaderParameter>();            });            app.UseSwagger();            app.UseSwaggerUI(c =>            {                c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiHelp V1");                c.RoutePrefix = string.Empty;            });
勾选生成
生成.png
还是不行,而后修改 web.config 文件 :

增加如下:

   <environmentVariables>     <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /></environmentVariables>

完整的:

<?xml version="1.0" encoding="utf-8"?><configuration>  <location path="." inheritInChildApplications="false">    <system.webServer>      <handlers>        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />      </handlers>      <aspNetCore processPath="dotnet" arguments=".\WebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="OutofProcess">                <environmentVariables>     <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /></environmentVariables>            </aspNetCore>    </system.webServer>  </location></configuration>
IIS 项目发布采用独立方式
独立.png

/template/Home/Zkeys/PC/Static