c# - 如何将静态 html 页面设置为 ASP.NET Core Web API 的主页?

标签 c# asp.net-core asp.net-core-webapi

我正在学习 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 页面放在这里:

enter image description here

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/

相关文章:

c# - 使用属性属性来定义要使用的 JsonConverter

c# - .NET Core API key 和身份集成

javascript - 在 ajax 查询中将 async 属性设置为 false 时,不会调用 Web Api Controller

c# - 关闭通用应用程序中打开的文件

c# - 在代码优先环境中从包管理器控制台更新数据库

c# - .NET Core 和 System.Drawing

c# - .NET Core 2.1 中 UserManager 的 AutoSaveChanges

c# - 我可以在不支付 Unity Multiplayer 服务的情况下使用 Unity 网络 HLAPI 吗?

c# - 如何将匿名类型的实例转换为 NameValueCollection

.net - HttpClient DelegatingHandler 意外的生命周期