Dotnet core uses static files in ASP.

Source: Internet
Author: User
Tags dotnet microsoft website

Use static files from Microsoft website in asp: https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/static-files;

providing static files

static files are typically located in web root ( <content-root>/Wwwroot ) folder. For more information, see content root and Web root . You typically set the content root to the current directory so that you can web root find the project during development.

public static void Main (string[] args) {    var host = new Webhostbuilder ()        . Usekestrel ()        . Usecontentroot (Directory.GetCurrentDirectory ())        . Useiisintegration ()        . Usestartup<startup> ()        . Build ();    Host. Run ();}

  

in order to provide static files, you must configure the middleware to add static files to the pipeline. You can Add dependencies for the Microsoft.AspNetCore.StaticFiles package to your project, and then UseStaticFiles from Startup.Configure The following method calls the extension method to configure the static file middleware:

public void Configure (Iapplicationbuilder app) {    app. Usestaticfiles ();}

for Access Test.png request, configure the static file middleware as follows:

public void Configure (Iapplicationbuilder app) {    app. Usestaticfiles (); For the Wwwroot folder    app. Usestaticfiles (New Staticfileoptions ()    {        Fileprovider = new Physicalfileprovider (            path.combine ( Directory.GetCurrentDirectory (), @ "Mystaticfiles"),        Requestpath = new PathString ("/staticfiles")    });

  StaticFileOptions()you can set the response header. For example, the following code sets the static file provided from the Wwwroot folder and sets the Cache-Control title so that it is publicly cacheable for 10 minutes (600 seconds):

public void Configure (Iapplicationbuilder app) {    app. Usestaticfiles (New Staticfileoptions ()    {        Onprepareresponse = ctx + = CTX            . Context.Response.Headers.Append ("Cache-control", "public,max-age=600");        }    );}

  

Enable Directory browsing

Directory Browsing allows users of your Web application to view a list of directories and files in a specified directory. For security reasons, directory browsing is disabled by default . To enable directory browsing, UseDirectoryBrowser Startup.Configure Call the extension method in the following ways:

 Public voidConfigure (Iapplicationbuilder app) {app. Usestaticfiles (); //For the wwwroot folderapp. Usestaticfiles (Newstaticfileoptions () {Fileprovider=NewPhysicalfileprovider (Path.Combine (Directory.GetCurrentDirectory (),@"wwwroot","Images")), Requestpath=NewPathString ("/myimages")    }); App. Usedirectorybrowser (Newdirectorybrowseroptions () {Fileprovider=NewPhysicalfileprovider (Path.Combine (Directory.GetCurrentDirectory (),@"wwwroot","Images")), Requestpath=NewPathString ("/myimages")    });}

and by calling AddDirectoryBrowser extension methods add the required services Startup.ConfigureServices :

 Public void configureservices (iservicecollection services) {    services. Adddirectorybrowser ();}

Fileextensioncontenttypeprovider

This FileExtensionContentTypeProvider class contains file extensions that map to a collection of MIME content types. In the following example, several file extensions are registered to a known MIME type, ". rtf" is replaced, ". mp4" is removed.

 Public voidConfigure (Iapplicationbuilder app) {//Set up custom content types-associating file extension to MIME type    varProvider =NewFileextensioncontenttypeprovider (); //ADD New MappingsProvider. mappings[". MyApp"] ="Application/x-msdownload"; Provider. mappings[". HTM3"] ="text/html"; Provider. mappings[". Image"] ="Image/png"; //Replace an existing mappingProvider. mappings[". rtf"] ="Application/x-msdownload"; //Remove MP4 videos.Provider. Mappings.remove (". mp4"); App. Usestaticfiles (Newstaticfileoptions () {Fileprovider=NewPhysicalfileprovider (Path.Combine (Directory.GetCurrentDirectory (),@"wwwroot","Images")), Requestpath=NewPathString ("/myimages"), Contenttypeprovider=provider}); App. Usedirectorybrowser (Newdirectorybrowseroptions () {Fileprovider=NewPhysicalfileprovider (Path.Combine (Directory.GetCurrentDirectory (),@"wwwroot","Images")), Requestpath=NewPathString ("/myimages")    });}
Non-standard content types

Asp. NET static file middleware to understand nearly 400 known file content types. If the user requests a file of unknown file type, the static file middleware returns an HTTP 404 (not found) response. If the Directory browsing feature is enabled, a link to the file is displayed, but the URI returns an HTTP 404 error.

The following code can provide an unknown type and render an unknown file as an image.

 Public void Configure (Iapplicationbuilder app) {    app. Usestaticfiles (new  staticfileoptions ()    {        true,        "  image/png"    });

Dotnet core uses static files in ASP.

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.