[ASP. NET Core] Static File Middleware, coremiddleware
Preface
This article introduces Middleware, which is used to process static files in ASP. NET Core, to keep a record for itself and hope to help developers who need it.
- ASP. NET Core Official Website
Structure
The most basic function of a Web platform is to put the Static File provided in the platform after receiving the HTTP Request packet sent from the browser ), the HTTP Response package content encapsulated as "server return" is provided to the browser.
In ASP. NET Core, a Middleware: StaticFileMiddleware is built in to create a Web platform to provide the static file function. This Middleware analyzes the URL path in the HTTP Request packet, calculates and obtains the File content under the corresponding File path according to the URL path, and then encapsulates the File content as the HTTP Response packet content., it is used by browsers.
In StaticFileMiddleware, two system parameters, URL root path and File root path, are defined to map the File path corresponding to the URL path. It is used to provide developers with the flexibility to set the relationship between the URL path and the File path.
Microsoft. AspNetCore. StaticFiles
In ASP. NET Core, you must add StaticFileMiddleware to provide the static file function. Developers can first follow the steps in the [ASP. NET Core] Getting Started article to establish the relevant environment and basic program code. Mount the reference "Microsoft. AspNetCore. StaticFiles" in project. json. The StaticFileMiddleware objects provided in this reference will be available later.
{ "version": "1.0.0-*", "buildOptions": { "debugType": "portable", "emitEntryPoint": true }, "dependencies": {}, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0" }, "Microsoft.AspNetCore.StaticFiles": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0" }, "imports": "dnxcore50" } }}
UseStaticFiles ()
After setting project. json, you can modify "Program. cs 」. UseStaticFiles Extension is provided in Microsoft. AspNetCore. StaticFiles, allowing developers to easily Mount StaticFileMiddleware. In the following sample code, demonstrate how to mount StaticFileMiddleware through UseStaticFiles. (In StaticFileMiddleware, the URL root path defaults to "http: // <Url>" and "File root path" defaults to "file :\\ <ContentRoot> \ wwwroot 」).
Using System; using System. IO; using Microsoft. aspNetCore. builder; using Microsoft. aspNetCore. hosting; using Microsoft. extensions. fileProviders; namespace aspnetcoreapp {public class Program {public static void Main (string [] args) {// Build var host = new WebHostBuilder () // set the File root path of the Host content. useContentRoot (Directory. getCurrentDirectory () // set the startup parameter. useStartup <Startup> () // enable Kestrel to listen to HTTP. useKestrel () // sets the URL of the listener. useUrls (" http://localhost:5000 ") // Create a Host. build (); // Run try {// start Host host. start (); // wait for the Console to be closed. writeLine ("Application started. press any key to shut down. "); Console. readKey ();} finally {// close Host host. dispose () ;}} public class Startup {// Methods public void Configure (IApplicationBuilder app) {// Mount StaticFilesMiddleware app. useStaticFiles ();}}}
UseWebRoot (webRoot)
In StaticFileMiddleware, the default File root path is file :\< ContentRoot> \ wwwroot 」. To change the default File root path, developers can use UseWebRoot Extension provided by ASP. NET Core to change the default File root path. In the following sample code, demonstrate how to change the default File root path through UseWebRoot. (StaticFileMiddleware mounted during example execution. The URL root path is also "http: // <Url>" and the File root path is changed to "file: \ <CurrentDirectory> \ aaa 」).
Using System; using System. IO; using Microsoft. aspNetCore. builder; using Microsoft. aspNetCore. hosting; using Microsoft. extensions. fileProviders; namespace aspnetcoreapp {public class Program {public static void Main (string [] args) {// Build var host = new WebHostBuilder () // set the File root path of the Web platform. useWebRoot (Directory. getCurrentDirectory () + @ "\ aaa") // set the File root path of the Host content. useContentRoot (Directory. getCurrentDirectory () // set the startup parameter. useStartup <Startup> () // enable Kestrel to listen to HTTP. useKestrel () // sets the URL of the listener. useUrls (" http://localhost:5000 ") // Create a Host. build (); // Run try {// start Host host. start (); // wait for the Console to be closed. writeLine ("Application started. press any key to shut down. "); Console. readKey ();} finally {// close Host host. dispose () ;}} public class Startup {// Methods public void Configure (IApplicationBuilder app) {// Mount StaticFilesMiddleware app. useStaticFiles ();}}}
UseStaticFiles (options)
In addition to mounting StaticFilesMiddleware with preset parameters, developers can also use custom parameters to mount StaticFilesMiddleware. To mount StaticFilesMiddleware with custom parameters, developers can also use UseStaticFiles Extension to mount StaticFilesMiddleware with custom parameters. In the following sample code, demonstrate how to mount StaticFilesMiddleware through UseStaticFiles and define its URL root path and File root path. (StaticFileMiddleware mounted when the example is executed. The URL root path is changed to "http: // <Url>/bbb" and the File root path is changed to "file: \ <CurrentDirectory> \ ccc 」).
Using System; using System. IO; using Microsoft. aspNetCore. builder; using Microsoft. aspNetCore. hosting; using Microsoft. extensions. fileProviders; namespace aspnetcoreapp {public class Program {public static void Main (string [] args) {// Build var host = new WebHostBuilder () // set the File root path of the Host content. useContentRoot (Directory. getCurrentDirectory () // set the startup parameter. useStartup <Startup> () // enable Kestrel to listen to HTTP. useKestrel () // sets the URL of the listener. useUrls (" http://localhost:5000 ") // Create a Host. build (); // Run try {// start Host host. start (); // wait for the Console to be closed. writeLine ("Application started. press any key to shut down. "); Console. readKey ();} finally {// close Host host. dispose () ;}} public class Startup {// Methods public void Configure (IApplicationBuilder app) {// Mount StaticFilesMiddleware app. useStaticFiles (new StaticFileOptions () {// set the URL root path RequestPath = @ "/bbb", // set the File root Directory FileProvider = new PhysicalFileProvider (Directory. getCurrentDirectory () + @ "\ ccc ")});}}}
Reference
- Working with Static Files-ASP. NET Core