[asp. Net Core] Static File Middleware

Source: Internet
Author: User
Tags hosting

Objective

This article describes the middleware of the asp. net core, which is used to deal with static files, leaving a record for itself and hoping to help developers in Need.

    • Asp. Net Core website
Structure
    • The most basic function of a web platform is to provide the browser with the static files provided in the platform after receiving the HTTP request packet from "browser", which is encapsulated as "server backhaul" HTTP response Packet Content.

    • In the asp. net core, a middleware:staticfilemiddleware is built to build the functionality of the Web site to provide static Files. This middleware will parse the URL path in the HTTP request packet, then compute the file content in the corresponding file path according to the URL path, and then encapsulate the file content as an HTTP response packet content for use by the Browser.

    • In staticfilemiddleware, the two system parameters, the URL root path and the file root path, are defined to image the file path corresponding to the URL path. To provide developers with the flexibility to set the relationship between the URL path and the file path.

Development Microsoft.AspNetCore.StaticFiles

In asp. net core, add Staticfilemiddleware to provide static file Function. Developers can build the relevant environment and basic program code in accordance with the steps in [asp. net Core] Getting started this article. Then in the Project.json to mount the reference "microsoft.aspnetcore.staticfiles", the following can be used in this reference, the Staticfilemiddleware related objects Provided.

{  "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 ()

Once you have finished setting up the project.json, you can go back and modify the "program.cs". In Microsoft.AspNetCore.StaticFiles, usestaticfiles Extension is provided, allowing developers to Mount Staticfilemiddleware Conveniently. In the sample program code below, demonstrate how to mount Staticfilemiddleware through Usestaticfiles. (in staticfilemiddleware, the URL root path defaults to: "http://<url>", the 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 (st                ring[] Args) {//Build var host = new Webhostbuilder ()//set The file root path of the host content . Usecontentroot (directory.getcurrentdirectory ())//set Startup Parameters. Usestartup<startup> ()//open Kestrel Listen to HTTP. Usekestrel ()//set The Listening url. Useurls ("http://localhost:5000")//establish Host.            Build (); Run Try {//start host Host.                Start (); Wait to close Console.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 the Staticfilesmiddleware app.        Usestaticfiles (); }    }}
Usewebroot (webRoot)

In staticfilemiddleware, the file root path defaults to: "file:\\<contentroot>\wwwroot". If you want to change the default file root path, developers can use the usewebroot extension provided by asp. Net core to change the default file root path. In the example program code below, demonstrate how to change the default file root path through Usewebroot. (the Staticfilemiddleware,url Root path that is mounted when the sample executes is also: "http://<url>", 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 (st                ring[] Args) {//Build var host = new Webhostbuilder ()//set The file root path of the web platform . Usewebroot (directory.getcurrentdirectory () + @ "\aaa")//sets The file root path of the host Content. Usecontentroot (directory.getcurrentdirectory ())//set Startup Parameters. Usestartup<startup> ()//open Kestrel Listen to HTTP. Usekestrel ()//set The Listening url. Useurls ("http://localhost:5000")//establish Host.            Build (); Run Try {//start host Host.                Start (); Wait to close Console.WriteLine ("application Started. Press any key to shut Down. ");            Console.readkey (); } finally {//closes the host Host.            Dispose ();        }}} public class Startup {//Methods public void Configure (iapplicationbuilder App) {//mount the Staticfilesmiddleware app.        Usestaticfiles (); }    }}
Usestaticfiles (options)

In addition to using preset parameters to mount staticfilesmiddleware, developers can use custom parameters to mount Staticfilesmiddleware. If you are using custom parameters to mount staticfilesmiddleware, developers can also use Usestaticfiles extension to mount Staticfilesmiddleware using custom Parameters. In the sample program code below, demonstrate how to mount Staticfilesmiddleware through usestaticfiles and define its URL root path and file root path. (the Staticfilemiddleware,url Root path that is mounted when the sample is executed is changed to: "http://<url>/bbb", file root path 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 (st                ring[] Args) {//Build var host = new Webhostbuilder ()//set The file root path of the host content . Usecontentroot (directory.getcurrentdirectory ())//set Startup Parameters. Usestartup<startup> ()//open Kestrel Listen to HTTP. Usekestrel ()//set The Listening url. Useurls ("http://localhost:5000")//establish Host.            Build (); Run Try {//start host Host.                Start (); Wait to close Console.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 the Staticfilesmiddleware app.                Usestaticfiles (new staticfileoptions () {//set url root Path requestpath = @ "/bbb",            Set file root Fileprovider = new Physicalfileprovider (directory.getcurrentdirectory () + @ "\ccc")        }); }    }}
Reference
    • Working with Static files-asp.net Core

[asp. Net Core] Static File Middleware

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.