ASP.NET Core Interview Question #87

What is UseStaticFiles()?

Middleware & Request Pipeline Mid-Level Intermediate

Quick Interview Answer

UseStaticFiles enables the static file middleware, which serves files such as CSS, JavaScript, images, and fonts directly to clients.

Detailed Explanation

UseStaticFiles enables the static file middleware, which serves files such as CSS, JavaScript, images, and fonts directly to clients.

By default, files are served from the application's web root, normally wwwroot. When a matching file is found, the middleware can short-circuit the pipeline and return it without invoking MVC or API endpoints.

Only files intended for public access should be placed in a publicly served directory.

Code Example

var app = builder.Build();

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();