So-called static files, including HTML files, CSS files, picture files and JS files, they are the server directly read to the client some resources, in this article, we will explain some of the contents of Asp.net5 and static files.
Static files on the server side
By default, the static file is stored in the project's Wwwroot directory, and the wwwroot address is defined in the Project.json file:
{ "Webroot": "Wwwroot", ...}
Static files are stored in any directory under Wwwroot and are accessed by the client as a relative path, for example, when you create a default Web application in Visual Studio, some folders are created in the Wwwroot directory: JS, images, CSS. Ask these questions directly. The path to a picture in the images directory should look like this:
HTTP// project address /images/picture name
For static files to be used, you must configure the middleware (middleware) to add static files in the pipeline (pipeline), which is done by invoking the app's Usestaticfiles in the Configure method in the Startup class:
Public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) { ... // Add static files to the request pipeline. app. Usestaticfiles (); ...
Now, let's say we have some static files in the project that you want to reference in your project, but it's outside the wwwroot, for example, the following:
In order for the user to have access to the Test.png file, you can configure the static file middleware as follows:
Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {...//Add mystaticfiles static files to the request pipeline.App. Usestaticfiles (Newstaticfileoptions () {Fileprovider=NewPhysicalfileprovider (@"D:\Source\WebApplication1\src\WebApplication1\MyStaticFiles"), Requestpath=NewPathString ("/staticfiles") }); ...
Once this is done, you can use Http://<yourApp>/StaticFiles/test.png to access the Test.png file described above.
Open Directory Browsing
Directory browsing allows the user of the application to see a list of files and directories for the specified directory, which is not enabled by default, and will receive an error if the user tries to display a directory. Developers can turn this feature on by invoking the app's Usedirectorybrower extension method:
Public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) { ... // Turn on directory browsing for the current directory. app. Usedirectorybrowser (); ...
Then, if you visit the app's images directory, it will be shown as:
Now, suppose we have a folder that wants to be accessed by the user but is located outside the wwwroot, in order to allow the user to see this folder through directory browsing, you can configure the static file middleware like this:
Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {...//ADD The ability for the user to browse the Mystaticfiles directory.App. Usedirectorybrowser (Newdirectorybrowserptions () {Fileprovider=NewPhysicalfileprovider (@"D:\Source\WebApplication1\src\WebApplication1\MyStaticFiles"), Requestpath=NewPathString ("/staticfiles") }); ...Render Default File
In order for your application to display a default page to the user without needing the URL full path, you can do so by calling the app's Usedefaultfiles extension method. Note that you must also call the Usestaticfiles method, because the Usedefaultfiles method simply rewrites the URL.
Public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) { ... // Serve The default file, if present. app. Usedefaultfiles (); App. Usestaticfiles (); ...
If your knowledge is so simple to call the Usedefaultfiles method and use a directory URL to access, then this middleware will search for one of the following files, if one of them is found, then this file will be displayed as the default file:
- Default.htm
- Default.html
- Index.htm
- Index.html
To allow you to specify a default file instead of the one described above, you can instantiate a Defaultfileoptions object and set its Defaultfilenames property to specify the default file. Then pass this object to the Usedefaultfiles method:
Public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) { ... // Serve My app-specific default file, if present. New defaultfilesoptions (); Options. Defaultfilenames.clear (); Options. Defaultfilenames.add ("mydefault.html"); App. Usedefaultfiles (options); App. Usestaticfiles (); ...
Now, if the user browses to the Webroot directory and there is a mydefault.html file in the directory, the file will be displayed by the browser.
But what if the default file you're showing is not in the Wwwroot directory? You can call the Usestaticfiles and Usedefaultfiles methods and pass the same values to both methods, and then we recommend using the Usefileserver method that will be described later in this article.
Using the Usefileserver method
As a complement to the usestaticfiles, Usedefaultfiles, and Usedirectorybrowser methods, there is a Usefileserver method that combines the functions of the first three methods, which we can use:
// enables all static file middleware (serving of static files, default files, and directory browsing). true);
Now, join the file you want to show outside the Wwwroot directory, instantiate and configure an Options object, and you can pass it as a parameter to the Usefileserver method. For example, there is now such a directory structure:
You might want to use a static file and set the default file and browse the Mystaticfiles directory, in the snippet below, you can just call a Usefileserver method to implement these requirements:
//Enable all static file middleware (serving of static files, default files,//and directory browsing) for the Mystaticfiles directory.App. Usefileserver (Newfileserveroptions () {Fileprovider=NewPhysicalfileprovider (@"D:\Source\WebApplication1\src\WebApplication1\MyStaticFiles"), Requestpath=NewPathString ("/staticfiles"), enabledirectorybrowsing=true});File type
The ASP. NET static file middleware defines nearly 400 file types and joins the user view to access a file type that the middleware does not contain, ASP. NET will not attempt to provide this file.
If the following directory structure is present:
With this directory structure, you can turn on directory browsing and static file access by using the methods described above, and you can http://localtion/images The directory sees the Test.image file, but when you click on this file, you will receive a 404 error-just as it really does not exist. To allow the display of these unknown types of files, You can set the Serveunknownfiletypes property of Staticfileoptions to True and set the appropriate content type for the Defaultcontenttype property (refer to the list of common MIME content types):
Public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) { ... // Serve static files and allow directory browsing. app. Usedirectorybrowser (); App. Usestaticfiles (new staticfileoptions { true, " Image/png " });
Now, if the browser tries to access an unknown type of file, the browser will render it as a picture.
So far, you've seen how to specify a default content type for a file type that is not recognized by ASP, however, what if you have multiple file types that are changed for ASP. Fortunately we have the Fileextensioncontenttypeprovider type.
Fileextensioncontenttypeprovider contains an internal list that maps to MIME content types and file suffixes, specifying a custom content type, Simply instantiate a Fileextensioncontenttypeprovider object and add a map to the Mappings property, as shown in the following article:
Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {...//Allow directory browsing.app. Usedirectorybrowser (); //Set up custom content types-associating file extension to MIME type varProvider =NewFileextensioncontenttypeprovider (); Provider. Mappings.add (". MyApp","Application/x-msdownload"); //Serve static files.App. Usestaticfiles (Newstaticfileoptions {Contenttypeprovider =provider}); ...IIS-based considerations
IIS users a local static file module that does not depend on the ASP. NET static file middleware component, which runs before the IIS local component, has a higher priority than the IIS local component, and in ASP. NET BETA 7, IIS has changed. So a request that is not being processed by ASP will return an empty 404 response instead of being executed by the IIS local module, and if you want to be handled by the IIS local module, add the following code at the end of the Configure method:
Public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) { ... ... // Enable the IIS native module to run after the ASP. Middleware. // This call should is placed at the end of your startup.configure method // it doesn ' t interfere with other middleware functionality. app. Runiispipeline ();}
Best practices
The code file should be placed outside the application's Webroot directory, which creates a complete isolation of the static file and source code.
Original address: http://docs.asp.net/en/latest/fundamentals/static-files.html
Various ways to use static files in Asp.net5