Tech Junkie Blog - Real World Tutorials, Happy Coding!: Enable ASP.NET Core to Serve Static Files

Thursday, July 14, 2016

Enable ASP.NET Core to Serve Static Files

As I have mentioned before ASP.NET Core decouples the application from the infrastructure as much as possible.  Therefore, you have to tell it exactly what you want in your project.  In this blog post we are going to tell ASP.NET that we want to serve static html files in our application.

Here are the steps to serve static files in our ASP.NET Core application.

1.  Open the "NorthwindCafe.Web" project, then click on the "Startup.cs" file in the project.  You will see the following markup in the Configure method

        public void Configure(IApplicationBuilder app)
            app.Run(async (context) =>
                await context.Response.WriteAsync("Hello World!");

2.  Go into the Configure method, remove the existing code and type in the following code
        public void Configure(IApplicationBuilder app)

You will notice that the app.UseStaticFiles() has a red underline on it. That's because there's a dependency that we are missing. Visual Studio is smart enough to detect that and give us a warning. You will see a yellow light bulb icon next to "app.UseStaticFiles()"

3. Click on the light bulb and select the first option which will add the namespace to the project and will automatically, once the file is saved.

 4. After the reference has been added you will see that in the "References" section you will see the "Microsoft.AspNetCore.StaticFiles" and an entry has been added to the project.json file for the package.  Now you can serve static files in the wwwroot directory

ASP.NET Core Posts:
  1. How To Create An ASP.NET Core Application From Scratch
  2. ASP.NET Core : Add jQuery, Bootstrap, AngularJS Using bower.json
  3. Enable ASP.NET Core to Serve Static Files
  4. Enable MVC On ASP.NET Core Application
  5. ASP.NET Core : Create Our First Controller and View  
  6. ASP.NET Core : Adding The Default View With _ViewStart.cshtml
  7. ASP.NET Core : Create A Responsive Layout With Bootstrap
  8. ASP.NET Core : Adding Font-Awesome For Northwind Cafe Navigation Icons
  9. ASP.NET Core : Add .json Configuration Files With Microsoft.Extensions.Configuration Library
  10. ASP.NET Core : Entity Framework Core Models For Northwind Cafe
  11. ASP.NET Core : Create The NothwindContext ( EntityFrameworkCore )
  12. ASP.NET Core : Configure project.json File To Support Entity Framework Core
  13. ASP.NET Core : Add NorthwindContext To Startup Class And Create Database
  14. ASP.NET Core: Seeding The NorwindCafe Database
  15. ASP.NET Core: Add Logging To The NorthwindCafe Application

1 comment:

Search This Blog