How ASP.NET Core serve static files?
In ASP.NET Core, Static files such as CSS, images, JavaScript files, HTML are the served directly to the clients. ASP.NET Core template provides a root folder called wwwroot
which contains all these static files. UseStaticFiles()
method inside Startup.Configure
enables the static files to be served to client.
You can serve files outside of this webroot folder by configuring Static File Middleware as following.
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, "MyStaticFiles")), // MyStaticFiles is new folder
RequestPath = "/StaticFiles" // this is requested path by client
});
// now you can use your file as below
<img src="/StaticFiles/images/profile.jpg" class="img" alt="A red rose" /> // profile.jpg is image inside MyStaticFiles/images
folder
Comments
Post a Comment