Static files, such as HTML, CSS, pictures and JavaScript, will be directly provided to clients by asp.net core applications.
Static file service
//Install staticfiles package
Install-Package Microsoft.AspNetCore.StaticFiles
In the application, we can enable the static file service by configuring the staticfiles middleware, and add the staticfiles middleware to the pipeline:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
}
Static files are usually located in the web root (/wwwroot) folder. We usually set the current directory of the project as content root, so that the web root of the project can be defined in the development phase.
- http://(app)/(fileName)
- http://localhost:9189/1.jpg
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")),
RequestPath = new PathString("/StaticFiles")
});
}
Static files can also be saved in any folder under the root directory of the website, and can be accessed through the relative root path.
- http://(app)/StaticFiles/(fileName)
- http://localhost:9189/StaticFiles/1.jpg
Read More