概要:本文經過示例,講解了 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"); }); } } } 複製代碼