10120
10120

Reputation: 135

How to set a static html page as homepage of an ASP.NET Core Web API?

I am learning ASP.NET Core Web API. I followed the tutorial and created an ASP.NET Core Web API project. but setting a static HTML page is not working.

This is my code:

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();

Basically, it's near to a newly created project. and I put the HTML page here:

enter image description here

Swagger and others all work well! but I cannot access to default HTML ("https://localhost:7184/default.html" is always 404)

Please help me!

Upvotes: 3

Views: 6293

Answers (2)

ahaghdan
ahaghdan

Reputation: 198

I think you need to add app.UseFileServer()

Excerpt from MSDN docs e.g. Static files in ASP.NET Core

Call app.UseFileServer to enable the serving of static files and the default file.

Your call to app.UseDefaultFiles() should not require the "/default.html" parameter since the documentation says:

With UseDefaultFiles, requests to a folder in wwwroot search for: default.htm default.html index.htm index.html

Upvotes: 4

CodingMytra
CodingMytra

Reputation: 2600

you need to reverse the order

app.UseStaticFiles();
app.UseDefaultFiles("/default.html");

updated:

you don't need to call app.UseDefaultFiles("/default.html"); at all

Upvotes: 3

Related Questions