In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The main content of this article is "NET Core2.0 static file directory related knowledge summary", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "NET Core2.0 static file directory related knowledge summary" bar!
Summary:
Through examples, this article explains the relevant knowledge of the NET Core2.0 static file directory, along with parsing, suitable for beginners, and complete project code. (the project uses the ASP.NET Core application initialized by vs2017, then select * empty project * *)
Sample code
Project structure
Program.cs file
Using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore;using Microsoft.AspNetCore.Hosting;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.Logging;namespace StaticFileServer {public class Program {public static void Main (string [] args) {BuildWebHost (args) .Run () } public static IWebHost BuildWebHost (string [] args) = > WebHost.CreateDefaultBuilder (args) .UseKestrel () .UseContentRoot (Directory.GetCurrentDirectory ()) / / sets the contents of the current directory .UseIISIntegration () .UseUr ls ("http://*:5000") / / causes the project to be accessed at port 5000 .UseStartup () .build ();}}
Startup.cs file
Using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.StaticFiles;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.FileProviders;namespace StaticFileServer {public class Startup {/ / This method gets called by the runtime. Use this method to add services to the container. / / For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices (IServiceCollection services) {} / / This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Public void Configure (IApplicationBuilder app, IHostingEnvironment env) {app.UseStaticFiles (); / / use the default folder wwwroot only shi wwwroot visible app.Run (async (context) = > {await context.Response.WriteAsync ("hello jesus");});}
Running effect:
Parsing: this is a basic static file server, and the app.UseStaticFiles () function makes the files in the default wwwroot in the current content directory accessible.
So the question is, what if you want to access static files in other directories?
Set the static files in any directory to access the code:
/ / set the files in the specified directory to be accessed start var staticfile = new StaticFileOptions (); staticfile.FileProvider = new PhysicalFileProvider (@ "C:\"); / / specify the directory, which refers to disk C, or you can specify another directory app.UseStaticFiles (staticfile)
Let's change the code of the * Configure*** function of startup.cs to the following code (the c disk file can be accessed):
/ / This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Public void Configure (IApplicationBuilder app, IHostingEnvironment env) {var staticfile = new StaticFileOptions (); staticfile.FileProvider = new PhysicalFileProvider (@ "C:\"); / / specify a directory, which refers to disk C, or you can specify another directory app.UseStaticFiles (staticfile); / / use the default folder wwwroot to only shi wwwroot visible app.Run (async (context) = > {await context.Response.WriteAsync ("hello jesus");});}
C disk file display
Running effect
In this way, we can access the files in any directory, so the problem is that there is a file called 456.log on disk c, which we can't access because: the server can't recognize it, so what should I do? How do you get the server to recognize all types of files? Let's take files with the suffix .log as an example.
Let's change * Configure*** to the following content:
/ / This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Public void Configure (IApplicationBuilder app, IHostingEnvironment env) {var staticfile = new StaticFileOptions (); staticfile.FileProvider = new PhysicalFileProvider (@ "C:\"); / / specify the directory, which refers to disk C, and you can also specify the file type corresponding to other directories / / settings (in case Mime type doesn't come out, cannot be opened or 404 error occurs) staticfile.ServeUnknownFileTypes = true; staticfile.DefaultContentType = "application/x-msdownload" / / set the default MIME TYPE var provider = new FileExtensionContentTypeProvider (); provider.Mappings.Add (".log", "text/plain"); / / manually set the corresponding MIME TYPE staticfile.ContentTypeProvider = provider; app.UseStaticFiles (staticfile); / / use the default folder wwwroot to only shi wwwroot the files in the specified directory can be accessed end app.Run (async (context) = > {await context.Response.WriteAsync ("hello jesus")) });}
We default to "application/x-msdownload" the unrecognized file types, that is, download all the unrecognized types that we have not dealt with.
Provider.Mappings.Add (".log", "text/plain"); / / manually set the corresponding MIME TYPE. We manually added the processing of file types with the suffix .log as text files, that is, txt processing.
Running effect
Unknown file (we access the 789.ggg file, which is a file type we have not processed)
File types processed
In this way, we can access any type of static files, so the problem comes again. I want to access all the files in a directory, that is, what if I access a directory?
The function of accessing directories in NET Core is disabled by default and needs to be turned on manually.
Steps:
1. Add directory access service to ConfigureServices function.
Public void ConfigureServices (IServiceCollection services) {services.AddDirectoryBrowser (); / / make the directory browsable (browse all files and folders)}
2. Add an intermediate key and a specific directory to the Configure function, where we make all directories under disk c accessible.
/ / set directory to browse start var dir = new DirectoryBrowserOptions (); dir.FileProvider = new PhysicalFileProvider (@ "C:\"); app.UseDirectoryBrowser (dir); / / set directory to browse end
In this way, we can access any directory on disk c, and the effect is as follows:
The final code of the Startup.cs file is as follows:
Using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.StaticFiles;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.FileProviders;namespace StaticFileServer {public class Startup {/ / This method gets called by the runtime. Use this method to add services to the container. / / For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices (IServiceCollection services) {services.AddDirectoryBrowser (); / / make the directory browsable (browse all files and folders)} / / This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Public void Configure (IApplicationBuilder app, IHostingEnvironment env) {/ / set directory browsable start var dir = new DirectoryBrowserOptions (); dir.FileProvider = new PhysicalFileProvider (@ "C:\"); app.UseDirectoryBrowser (dir); / / set directory browsable end / / files in specified directory can be accessed start var staticfile = new StaticFileOptions (); staticfile.FileProvider = new PhysicalFileProvider (@ "C:\") / / specify a directory, which refers to disk C, and you can also specify other directories / / to set the corresponding file type (to prevent Mime type from coming out, cannot be opened or 404 error occurs) staticfile.ServeUnknownFileTypes = true; staticfile.DefaultContentType = "application/x-msdownload"; / / set the default MIME TYPE var provider = new FileExtensionContentTypeProvider (); provider.Mappings.Add (".log", "text/plain") / / manually set the corresponding MIME TYPE staticfile.ContentTypeProvider = provider; app.UseStaticFiles (staticfile); / / use the default folder wwwroot only shi wwwroot is visible / / set files in the specified directory can be accessed end app.Run (async (context) = > {await context.Response.WriteAsync ("hello jesus");}) At this point, I believe you have a deeper understanding of the "summary of relevant knowledge of the NET Core2.0 static file directory". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.