Introduction
This article explains about static files and how to set default pages using static files in ASP.NET using Visual Studio 2017 in a step by step way. Before reading this article, please read the previous parts at the below link.
Static Files
HTML, CSS, images, JavaScript files are called static files. These files are located in wwwroot folder insight our project. We directly access our static files which are in wwwroot folder, if not in wwwroot we cannot access directly.
Background
For some of the files for which we need to access directly from one place to another we use wwwroot folder.
Steps for Creating Static Page
Open New Project
We are using Visual Studio 2017 for developing ASP.NET Core application. Open visual studio and open new project then select the “ASP.NET Core Web Application (.NET Core)”.
We select the “ASP.NET Core 1.1” and select Empty template. Use an empty project template for creating an ASP.NET Core application. This template does not have any content in it.
Add Static File Packages
In our project, we need to add the Microsoft.AspNetCore.StaticFiles packages in our project to serve the static page in our ASP.Net core applications. Go to solution explorer, right click on our project, and go to Manage NuGet Packages.
In NuGet Package manager, go to browse and type static files in search textbox and we can find the “Microsoft.AspNetCore.StaticFiles” package. Select “Microsoft.AspNetCore.StaticFiles” packages and click the install button on the right sight.
Select the version 1.1.1 and click install then review changes and click OK. Now Licenses Acceptance window will open then click the I accept button then installation will be complete.
We can see that the package in NuGet inside the Dependencies folder looks like below screenshot.
Add HTML File in wwwroot
We are going to add sample HTML file in wwwroot folder. Go to solution explorer, right click on wwwroot go to Add then go to New Item.
Select HTML Page and give page name then click Add button.
We are adding some sample code in Home.html page. We are going to make Home.html as a home page using “Microsoft.AspNetCore.StaticFiles” packages.
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>ASP.NET Core</title>
- </head>
- <body>
- <h1>Welcome To ASP.NET Core Static Page</h1>
- </body>
- </html>
Add Code in Startup class
Go to Startup class and enable default file mapping on the current path then enable static file serving for the current path. We need to add following code in startup class at Configure method.
- public void Configure(
- IApplicationBuilder app,
- IHostingEnvironment env,
- ILoggerFactory loggerFactory
- )
- {
- loggerFactory.AddConsole();
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
-
-
- DefaultFilesOptions options = new DefaultFilesOptions();
- options.DefaultFileNames.Clear();
- options.DefaultFileNames.Add("Home.html");
- app.UseDefaultFiles(options);
-
-
- app.UseStaticFiles();
-
- app.Run((context) =>
- {
-
- throw new Exception();
- });
- }
Now build the ASP.NET Core application and run the application. When we run the application it directly redirects the Home.html page.
Not Directly Redirect Page
If we remove some lines in above-written code from Startup class in configure method, the page does not redirect to the Home.html page. For example, we modified the below code in configure method and ran the application.
- public void Configure(
- IApplicationBuilder app,
- IHostingEnvironment env,
- ILoggerFactory loggerFactory
- )
- {
- loggerFactory.AddConsole();
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
-
- app.Run((context) =>
- {
-
- throw new Exception();
- });
- }
We get some of the errors in the output page and we can see it looks like the below screenshot.
If we enter “http://localhost:53498/home.html” manually then we get the same error. Now add a single line of code from the above-written code and it looks like below.
- public void Configure(
- IApplicationBuilder app,
- IHostingEnvironment env,
- ILoggerFactory loggerFactory
- )
- {
- loggerFactory.AddConsole();
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
-
-
- app.UseStaticFiles();
-
- app.Run((context) =>
- {
-
- throw new Exception();
- });
- }
Here we mentioned “app.UseStaticFiles();” . It denotes enabling the static file for the current request path. If entered manually like “http://localhost:53498/home.html” after running the application it will redirect the home.html page.
If page redirects to the Home.html Page we need to add “app.UseDefaultFiles()” before the “app.UseStaticFiles();”. The UseDefaultFiles() only searches on the below static file page names
- default.htm
- default.html
- index.htm
- index.html
We are creating a different name and we need to mention specifically to UseDefaultFiles(). We can add the below code to mention the specific file name.
-
- DefaultFilesOptions options = new DefaultFilesOptions();
- options.DefaultFileNames.Clear();
- options.DefaultFileNames.Add("Home.html");
- app.UseDefaultFiles(options);
After building and running the code it will run successfully. Now the page automatically redirects to the Home.html page.
Conclusion
This article explained about how to serve static files in ASP.Net Core. I hope this article explained in a very simple way and is very useful to new learners.