ASP.NET CORE 5.0 Web API在IIS Express上工作,但在IIS 10上托管时提供404



我在一个解决方案中有一个WEB应用程序和一个WEB API项目。最近,我们将Swashbuckle和Swagger UI添加到API项目中。现在,当使用IISExpress在VS中运行表单(调试/不调试(时,Web API工作正常,Swagger.json是可访问的,SwaggerUI在https://localhost:/API/docs/index.html并尝试https://localhost:/API/docs/,返回有效输出。

但是,在发布并部署到IIS网站后,它会返回404错误。Swagger.json已经形成,Swagger UI在https://domain:/API/docs/index.html但在尝试时https://domain:/API/docs/返回404。(DNS映射正确(。

IIS 10.0详细错误-404.0-找不到

Detailed Error Information: 
Module        IIS Web Core 
Notification MapRequestHandler 
Handler       StaticFile 
Error Code   0x80070002 
Requested URL https://localhost:<port>/Rejected-By-UrlScan?~/API/docs/<command> 
Physical Path <path>iisabcRejected-By-UrlScan 
Logon Method Anonymous 
Logon User      Anonymous

通过CLI发布成功

dotnet publish -c release -r win-x64 --self-contained false

启动.cs

public void ConfigureServices(IServiceCollection services)
{








services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme)
.AddCertificate();
services.Configure<ApplicationSetting>(Configuration.GetSection("ApplicationSettings"));
services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));

services.AddControllers()
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressModelStateInvalidFilter = true;
});

services.AddMvcCore().SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
services.AddMvcCore()
.AddApiExplorer();
abcDBContext.ConnectionString = Configuration.GetConnectionString("abcDBContext");
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = API",
Description = "Web API",
TermsOfService = new Uri("https://example.com/terms"),
Contact = new OpenApiContact
{
Name = "ABC Support",
Email = string.Empty,
Url = new Uri("https://example.com/"),
},
License = new OpenApiLicense
{
Name = "Use under LICX",
Url = new Uri("https://example.com/license"),
}
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
DependencyInjections.Dependency(services,Configuration);

}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
if (env.IsProduction() || env.IsStaging())
{
app.UseExceptionHandler("/Error/index.html");
}
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger(c =>
{
c.RouteTemplate = "docs/{documentName}/swagger.json";
});
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "docs"; 
c.SwaggerEndpoint("v1/swagger.json", "v1");
// custom CSS
c.InjectStylesheet("/swagger-ui/custom.css");
});
app.Use(async (ctx, next) =>
{
await next();
if (ctx.Response.StatusCode == 204)
{
ctx.Response.ContentLength = 0;
}
});

app.UseCors(builder =>
builder.WithOrigins(Configuration["ApplicationSettings:Client_URL"].ToString())
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
);
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseCors();
}

LaunchSettings.json

{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:2618",
"sslPort": 44311
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "docs",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Web.API": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "docs",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

操作系统:Windows 10最新版本x64

IIS站点(使用相同的应用程序池(

应用程序池详细信息

IIS站点

我通过卸载URLScan 3.1解决了这个问题,基于Lex Li共享的(链接(,从这里开始

相关内容

最新更新