概要:本文经过示例,讲解了 NET Core2.0 静态文件目录的相关知识,并附带解析,适合新手,并附带了完整的项目代码。(项目经过 vs2017 初始化的 ASP.NET Core 应用程序,以后选择***空项目***)bash
项目结构 服务器
program.cs文件app
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()) // 设置当前目录的内容
.UseIISIntegration()
.UseUrls("http://*:5000") // 使 项目在 5000端口被访问
.UseStartup<Startup>()
.Build();
}
}
复制代码
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(); // 使用默认文件夹 wwwroot 仅仅shi wwwroot对外可见
app.Run(async (context) =>
{
await context.Response.WriteAsync("hello jesus");
});
}
}
}
复制代码
那么问题来了,若想访问其余目录下的静态文件,该怎么办?async
// 设置 指定目录的文件 能够被访问 start
var staticfile = new StaticFileOptions();
staticfile.FileProvider = new PhysicalFileProvider(@"C:\"); // 指定目录,这里指的是C盘,也能够指定其余目录 app.UseStaticFiles(staticfile); 复制代码
咱们吧startup.cs的***Configure*** 函数代码改成以下代码(增长了c盘文件能够访问):ide
// 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:\"); // 指定目录,这里指的是C盘,也能够指定其余目录 app.UseStaticFiles(staticfile); // 使用默认文件夹 wwwroot 仅仅shi wwwroot对外可见 app.Run(async (context) => { await context.Response.WriteAsync("hello jesus"); }); } 复制代码
这样咱们就能够访问任意目录下的文件了,那么问题来了,c盘中有个 叫 456.log 的文件,咱们访问不了,缘由是:服务器不能识别,怎么办?如何让服务器识别 全部类型的文件呢? 咱们以 .log 为后缀的文件为例函数
// 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:\"); // 指定目录,这里指的是C盘,也能够指定其余目录 // 设置 对应的文件类型(防止Mime type没事别出来,打不开或出现404错误) staticfile.ServeUnknownFileTypes = true; staticfile.DefaultContentType = "application/x-msdownload";// 设置默认 MIME TYPE var provider = new FileExtensionContentTypeProvider(); provider.Mappings.Add(".log", "text/plain"); // 手动设置对应的 MIME TYPE staticfile.ContentTypeProvider = provider; app.UseStaticFiles(staticfile); // 使用默认文件夹 wwwroot 仅仅shi wwwroot对外可见 // 设置 指定目录的文件 能够被访问 end app.Run(async (context) => { await context.Response.WriteAsync("hello jesus"); }); } 复制代码
咱们将不能识别的文件类型默认为 : "application/x-msdownload",即遇到咱们没处理的,不能识别的类型通通下载下来。 provider.Mappings.Add(".log", "text/plain"); // 手动设置对应的 MIME TYPE 。咱们手动增长了 对后缀为.log的文件类型的处理,当成文本文件处理,即txt处理。ui
这样,咱们就能够访问任意类型的静态文件了,那么问题又来了, 我想访问一个目录下全部的文件,即访问某个目录怎么办?this
在 NET Core 中访问目录的功能默认是禁止的,须要手动开启。 步骤:spa
public void ConfigureServices(IServiceCollection services)
{
services.AddDirectoryBrowser(); // 使目录能够被浏览 (浏览全部的文件以及文件夹)
}
复制代码
// 设置 目录可浏览 start
var dir = new DirectoryBrowserOptions();
dir.FileProvider = new PhysicalFileProvider(@"C:\"); app.UseDirectoryBrowser(dir); // 设置 目录可浏览 end 复制代码
这样咱们就能够访问c盘中的任意目录了,效果以下: 3d
Startup.cs 文件最终代码以下:
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(); // 使目录能够被浏览 (浏览全部的文件以及文件夹)
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// 设置 目录可浏览 start
var dir = new DirectoryBrowserOptions();
dir.FileProvider = new PhysicalFileProvider(@"C:\"); app.UseDirectoryBrowser(dir); // 设置 目录可浏览 end // 设置 指定目录的文件 能够被访问 start var staticfile = new StaticFileOptions(); staticfile.FileProvider = new PhysicalFileProvider(@"C:\"); // 指定目录,这里指的是C盘,也能够指定其余目录 // 设置 对应的文件类型(防止Mime type没事别出来,打不开或出现404错误) staticfile.ServeUnknownFileTypes = true; staticfile.DefaultContentType = "application/x-msdownload";// 设置默认 MIME TYPE var provider = new FileExtensionContentTypeProvider(); provider.Mappings.Add(".log", "text/plain"); // 手动设置对应的 MIME TYPE staticfile.ContentTypeProvider = provider; app.UseStaticFiles(staticfile); // 使用默认文件夹 wwwroot 仅仅shi wwwroot对外可见 // 设置 指定目录的文件 能够被访问 end app.Run(async (context) => { await context.Response.WriteAsync("hello jesus"); }); } } } 复制代码