ASP. NET Core static File Usage tutorial (9), asp. netcore
In this chapter, we will learn how to use files. Almost every web application requires an important feature: providing files (static files) from the file system ).
- Static files such as JavaScript files, images, and CSS files can be directly provided to customers through Asp. Net Core applications.
- Static files are usually located in the wwwroot folder.
- By default, this is the only place where we can provide files directly from the file system.
Case
Now let's take a simple example to learn how we provide these static files in our application.
Here, we want to add a simple HTML file to our FirstAppDemo application, which is placed in the wwwroot folder. In Solution Explorer, right-click the wwwroot folder and choose Add> new project.
In the middle pane, select the HTML page and call it index.html. Click the Add button.
You will see a simple index.html file. Let's add some simple text and titles as follows.
<!DOCTYPE html>
When you Run the application and input index.html in the browser, you will see that app. Run middleware throws an exception because there is nothing in our application currently.
Currently, no middleware in our project will find any files on the file system.
To solve this problem, right-click your project in Solution Explorer and choose manage NuGet packages to go to The NuGet Package Manager.
Search for Microsoft. AspNet. StaticFiles and find the static file middleware. Let's install this nuget package. Now we can register middleware in the Configure method.
Let's add UseStaticFiles middleware in the Configure method shown in the following program.
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; namespace FirstAppDemo { public class Startup { public Startup() { var builder = new ConfigurationBuilder() .AddJsonFile("AppSettings.json"); Configuration = builder.Build(); } public IConfiguration Configuration { get; set; } // This method gets called by the runtime. // Use this method to add services to the container. // For more information on how to configure your application, // visit http://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { } // This method gets called by the runtime. // Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { app.UseIISPlatformHandler(); app.UseDeveloperExceptionPage(); app.UseRuntimeInfoPage(); app.UseStaticFiles(); app.Run(async (context) => { throw new System.Exception("Throw Exception"); var msg = Configuration["message"]; await context.Response.WriteAsync(msg); }); } // Entry point for the application. public static void Main(string[] args) => WebApplication.Run<Startup>(args); } }
Unless you overwrite the options by passing in some different configuration parameters, the static file will regard a given request as a request path. The Request Path is relative to the file system.
- If a static file finds a file based on the url, it will directly return the file without calling the next middleware.
- If no matching file is found, it will continue to execute the next middleware.
Let's save the Startup. cs file and refresh the browser.
Now you can see the index.html file. Any JavaScript files, CSS files, or HTML files stored in the wwwroot folder can be directly used as static files in Asp. Net Core.
- If you want to make index.html your default file, IIS always has this function.
- You can give IIS a default file list. If someone accesses the root directory, in this case, if IIS finds the file named index.html, it will automatically return the file to the client.
- Let's start a few changes now. First, we need to delete the forced error and add another middleware, Which is UseDefaultFiles. The following is the implementation of the configuration method.
/ This method gets called by the runtime. // Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { app.UseIISPlatformHandler(); app.UseDeveloperExceptionPage(); app.UseRuntimeInfoPage(); app.UseDefaultFiles(); app.UseStaticFiles(); app.Run(async (context) => { var msg = Configuration["message"]; await context.Response.WriteAsync(msg); }); }
This middleware will listen to incoming requests. If the request is a root directory, check whether there are matching default files.
You can overwrite the contents of this median to specify a matching file, but index.html is a default file by default.
Let's save the Startup. cs file and transfer your browser to the root directory of the web application.
Now you can see that index.html is the default file. The order in which you install the middleware is very important, because if you place UseDefaultFiles after UseStaticFiles, you may not get the same result.
If you want to use UseDefaultFiles and UseStaticFiles middleware, you can use another middleware, Microsoft. aspnet. staticfiles, which is also a NuGet package and is a server middleware. This essentially contains the default and static files in the correct order.
// This method gets called by the runtime. // Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { app.UseIISPlatformHandler(); app.UseDeveloperExceptionPage(); app.UseRuntimeInfoPage(); app. UseFileServer(); app.Run(async (context) => { var msg = Configuration["message"]; await context.Response.WriteAsync(msg); }); }
Let's save the Startup. cs file again. Once you refresh the browser, you will see the same result, as shown in the screen snapshot below.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.