我正在学习 ASP.NET Core Web API。我关注了tutorial并创建了一个 ASP.NET Core Web API 项目。但设置静态 HTML 页面不起作用。
这是我的代码:
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "abc's API",
Description = "abc's Site directory API",
Version = "V1",
TermsOfService = new Uri("https://github.com/abc"),
Contact = new OpenApiContact
{
Name = "abc xyz",
Url = new Uri("https://github.com/abc")
},
License = new OpenApiLicense
{
Name = "Example License",
Url = new Uri("https://example.com/license")
}
});
// using System.Reflection;
string xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
string xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFilename);
options.IncludeXmlComments(xmlPath);
options.SchemaFilter<EnumSchemaFilter>();
});
builder.Services.Configure<AuthSetting>(builder.Configuration.GetRequiredSection(AuthSetting.AuthSection));
builder.Services.Configure<SPSetting>(builder.Configuration.GetRequiredSection(SPSetting.SPSection));
// Azure log system configuration, default location : D:\\home\\LogFiles\\Application
builder.Logging.AddAzureWebAppDiagnostics();
builder.Services.Configure<AzureFileLoggerOptions>(options =>
{
options.FileName = "azure-diagnostics-abc";
options.FileSizeLimit = 50 * 1024;
options.RetainedFileCountLimit = 5;
});
builder.Services.Configure<AzureBlobLoggerOptions>(options => { options.BlobName = "abc-log.txt"; });
WebApplication app = builder.Build();
// Configure the HTTP request pipeline.
app.UseDefaultFiles("/default.html");
app.UseStaticFiles();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.RoutePrefix = "abc";
});
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
基本上,它接近于一个新创建的项目。我将 HTML 页面放在这里:
Swagger 和其他人都工作得很好!但我无法访问默认 HTML(“https://localhost:7184/default.html”始终为 404)
请帮帮我!
最佳答案
你需要颠倒顺序
app.UseStaticFiles();
app.UseDefaultFiles("/default.html");
更新:
你根本不需要调用 app.UseDefaultFiles("/default.html");
关于c# - 如何将静态 html 页面设置为 ASP.NET Core Web API 的主页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72948358/