To get started with static files of asp.net core, asp. netcore

Source: Internet
Author: User

To get started with static files of asp.net core, asp. netcore

This article mainly describes the static resource processing solution in asp.net core:

I. static file service

First, clarify the concepts of contentRoot and webroot.

  • ContentRoot: The project folder of the web, which contains other folders such as webroot and other bin.
  • Webroot: webroot is a site folder that can be accessed by URLs. Default Value: "contentroot/wwwroot"
  • The implementation code is as follows:
    Code in Program
Public static IWebHost BuildWebHost (string [] args) => WebHost. createdefabuilder Builder (args ). useStartup <Startup> (). useKestrel (). useContentRoot (Directory. getCurrentDirectory () // set contentroot. useWebRoot ("mywwwroot") // sets webroot. useUrls ("http: // *: 5000") // other computers can use IP addresses for access. build ();

Code in StartUp

Public void Configure (IApplicationBuilder app, IHostingEnvironment env) {app. useStaticFiles (); // enable static file access // custom static file access app. useStaticFiles (new StaticFileOptions () {FileProvider = new PhysicalFileProvider (Path. combine (Directory. getCurrentDirectory (), "mystatic"), RequestPath = new PathString ("/sam/static")}); // configure the mvc app. useMvc (routers => {routers. mapRoute ("default", "{controller = Home}/{action = Index }/ {Id ?} ");});}

:

1.1 directory browsing

The implementation code is as follows:

public void Configure(IApplicationBuilder app, IHostingEnvironment env){      app.UseDirectoryBrowser(new DirectoryBrowserOptions(){        FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Controllers")),        RequestPath = new PathString("/controller")    }); }

1.2 default document

The app. UseDefaultFiles method enables the default access configuration. The configuration item is represented by the DefaultFilesOption class. The Code is as follows:

Public void Configure (IApplicationBuilder app, IHostingEnvironment env) {// default file ultultfilesoptions defaultFiles = new DefaultFilesOptions (); defaultFiles. defaultFileNames. clear (); defaultFiles. defaultFileNames. add ("myindex.html"); app. useDefaultFiles (defaultFiles); app. useStaticFiles (); // enable static file access}

Note that this configuration must be before all Use operations; otherwise, the configuration will not take effect.

1.3 UseFileServer

UserFileServer includes UseStaticFiles, UseDefaultFiles, and UserDirectoryBrowser functions.

App. useFileServer (new FileServerOptions () {EnableDefaultFiles, // whether to enable the default file EnableDirectoryBrowsing, // whether to enable directory browsing DefaultFilesOptions, // set StaticFileOptions in the default file, // set DirectoryBrowserOptions for static resource access, // directory browsing settings });
Ii. static file authorization

The static module does not perform permission checks on files, including files and folders under wwwroot. For permission control, you can use action to return a FileResult:

private string basePath = Common.Uitls.HostingEnvironment.ContentRootPath;public FileResult Index(int id){    if(id == 1){        return new PhysicalFileResult(Path.Combine(basePath, "aufolder","author.html"), "text/html");    }    return new PhysicalFileResult(Path.Combine(basePath, "error.html"), "text/html");;}
Iii. Use of the FileExtensionContentTypeProvider class

This class contains a set that maps file extensions to MIME content types. The Code is as follows:

FileExtensionContentTypeProvider provider = new FileExtensionContentTypeProvider (); provider. mappings. add (". sam "," text/plain "); // custom static file access app. useStaticFiles (new StaticFileOptions () {FileProvider = new PhysicalFileProvider (Path. combine (Directory. getCurrentDirectory (), "mystatic"), RequestPath = new PathString ("/sam/static"), ContentTypeProvider = provider });
  • The FileExtensionContentTypeProvider class is associated with UseStaticFiles
  • The extension starts.
  • The running result is as follows:

Related Article

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.