c# .Net Core靜態(tài)文件服務(wù)器的新人入門教程
概要:
本文通過示例,講解了 NET Core2.0 靜態(tài)文件目錄的相關(guān)知識,并附帶解析,適合新手,并附帶了完整的項(xiàng)目代碼。(項(xiàng)目通過 vs2017 初始化的 ASP.NET Core 應(yīng)用程序,之后選擇***空項(xiàng)目***)
示例代碼
項(xiàng)目結(jié)構(gòu)
program.cs文件
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()) // 設(shè)置當(dāng)前目錄的內(nèi)容 .UseIISIntegration() .UseUrls("http://*:5000") // 使 項(xiàng)目在 5000端口被訪問 .UseStartup<Startup>() .Build(); } }
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) { } // 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(); // 使用默認(rèn)文件夾 wwwroot 僅僅shi wwwroot對外可見 app.Run(async (context) => { await context.Response.WriteAsync("hello jesus"); }); } } }
運(yùn)行效果:
解析: 這是一個基本的靜態(tài)文件服務(wù)器,app.UseStaticFiles() 函數(shù)使當(dāng)前內(nèi)容目錄下默認(rèn)的 wwwroot中的文件可以被訪問
那么問題來了,若想訪問其他目錄下的靜態(tài)文件,該怎么辦?
設(shè)置任意目錄下的靜態(tài)文件可以訪問代碼:
// 設(shè)置 指定目錄的文件 可以被訪問 start var staticfile = new StaticFileOptions(); staticfile.FileProvider = new PhysicalFileProvider(@"C:\"); // 指定目錄,這里指的是C盤,也可以指定其他目錄 app.UseStaticFiles(staticfile);
我們吧startup.cs的***Configure*** 函數(shù)代碼改為如下代碼(增加了c盤文件可以訪問):
// 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); // 使用默認(rèn)文件夾 wwwroot 僅僅shi wwwroot對外可見 app.Run(async (context) => { await context.Response.WriteAsync("hello jesus"); }); }
c盤文件展示
運(yùn)行效果
這樣我們就可以訪問任意目錄下的文件了,那么問題來了,c盤中有個 叫 456.log 的文件,我們訪問不了,原因是:服務(wù)器不能識別,怎么辦?如何讓服務(wù)器識別 所有類型的文件呢? 我們以 .log 為后綴的文件為例
我們將***Configure*** 改為一下內(nèi)容:
// 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盤,也可以指定其他目錄 // 設(shè)置 對應(yīng)的文件類型(防止Mime type沒事別出來,打不開或出現(xiàn)404錯誤) staticfile.ServeUnknownFileTypes = true; staticfile.DefaultContentType = "application/x-msdownload";// 設(shè)置默認(rèn) MIME TYPE var provider = new FileExtensionContentTypeProvider(); provider.Mappings.Add(".log", "text/plain"); // 手動設(shè)置對應(yīng)的 MIME TYPE staticfile.ContentTypeProvider = provider; app.UseStaticFiles(staticfile); // 使用默認(rèn)文件夾 wwwroot 僅僅shi wwwroot對外可見 // 設(shè)置 指定目錄的文件 可以被訪問 end app.Run(async (context) => { await context.Response.WriteAsync("hello jesus"); }); }
我們將不能識別的文件類型默認(rèn)為 : "application/x-msdownload",即遇到我們沒處理的,不能識別的類型統(tǒng)統(tǒng)下載下來。
provider.Mappings.Add(".log", "text/plain"); // 手動設(shè)置對應(yīng)的 MIME TYPE 。我們手動增加了 對后綴為.log的文件類型的處理,當(dāng)成文本文件處理,即txt處理。
運(yùn)行效果
未知的文件 (我們訪問789.ggg文件,此文件類型我們未處理過)
已處理的文件類型
這樣,我們就可以訪問任意類型的靜態(tài)文件了,那么問題又來了, 我想訪問一個目錄下所有的文件,即訪問某個目錄怎么辦?
在 NET Core 中訪問目錄的功能默認(rèn)是禁止的,需要手動開啟。
步驟:
1、在 ConfigureServices 函數(shù)中增加 目錄訪問服務(wù),
public void ConfigureServices(IServiceCollection services) { services.AddDirectoryBrowser(); // 使目錄可以被瀏覽 (瀏覽所有的文件以及文件夾) }
2、在Configure 函數(shù)中增加 中間鍵 和 具體的目錄,在這里我們讓 c盤下的所有目錄可以被訪問
// 設(shè)置 目錄可瀏覽 start var dir = new DirectoryBrowserOptions(); dir.FileProvider = new PhysicalFileProvider(@"C:\"); app.UseDirectoryBrowser(dir); // 設(shè)置 目錄可瀏覽 end
這樣我們就可以訪問c盤中的任意目錄了,效果如下:
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) { // 設(shè)置 目錄可瀏覽 start var dir = new DirectoryBrowserOptions(); dir.FileProvider = new PhysicalFileProvider(@"C:\"); app.UseDirectoryBrowser(dir); // 設(shè)置 目錄可瀏覽 end // 設(shè)置 指定目錄的文件 可以被訪問 start var staticfile = new StaticFileOptions(); staticfile.FileProvider = new PhysicalFileProvider(@"C:\"); // 指定目錄,這里指的是C盤,也可以指定其他目錄 // 設(shè)置 對應(yīng)的文件類型(防止Mime type沒事別出來,打不開或出現(xiàn)404錯誤) staticfile.ServeUnknownFileTypes = true; staticfile.DefaultContentType = "application/x-msdownload";// 設(shè)置默認(rèn) MIME TYPE var provider = new FileExtensionContentTypeProvider(); provider.Mappings.Add(".log", "text/plain"); // 手動設(shè)置對應(yīng)的 MIME TYPE staticfile.ContentTypeProvider = provider; app.UseStaticFiles(staticfile); // 使用默認(rèn)文件夾 wwwroot 僅僅shi wwwroot對外可見 // 設(shè)置 指定目錄的文件 可以被訪問 end app.Run(async (context) => { await context.Response.WriteAsync("hello jesus"); }); } } }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- 解析如何利用一個ASP.NET Core應(yīng)用來發(fā)布靜態(tài)文件
- .Net Core中間件之靜態(tài)文件(StaticFiles)示例詳解
- .NET Core單文件發(fā)布靜態(tài)編譯AOT CoreRT的方法詳解
- ASP.NET Core靜態(tài)文件的使用方法
- ASP.NET Core靜態(tài)文件使用教程(9)
- ASP.NET Core中預(yù)壓縮靜態(tài)文件的方法步驟
- 1個文件如何輕松搞定Asp.net core 3.1動態(tài)頁面轉(zhuǎn)靜態(tài)頁面
- ASP.NET Core 應(yīng)用程序中的靜態(tài)文件中間件的實(shí)現(xiàn)
- 淺談ASP.NET Core靜態(tài)文件處理源碼探究
- asp .net core靜態(tài)文件資源的深入講解
相關(guān)文章
asp.net下創(chuàng)建、查詢、修改帶名稱空間的 XML 文件的例子
asp.net下創(chuàng)建、查詢、修改帶名稱空間的 XML 文件的例子...2007-04-04asp.net使用Socket.Send發(fā)送信息及Socket.SendFile傳輸文件的方法
這篇文章主要介紹了asp.net使用Socket.Send發(fā)送信息及Socket.SendFile傳輸文件的方法,結(jié)合實(shí)例形式分析了asp.net基于socket實(shí)現(xiàn)信息與文件傳輸?shù)南嚓P(guān)技巧,需要的朋友可以參考下2016-06-06Entity?Framework代碼優(yōu)先Code?First入門
這篇文章介紹了Entity?Framework的代碼優(yōu)先模式Code?First,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06ASP.net實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法
在ASP.NET進(jìn)行頁面開發(fā)時候經(jīng)常遇到需要進(jìn)行頁面跳轉(zhuǎn)的操作。這個其實(shí)并不難,關(guān)鍵是知不知道的問題。下面給出操作方法。2013-06-06調(diào)試ASP.NET應(yīng)用程序的方法和技巧
調(diào)試ASP.NET應(yīng)用程序的方法和技巧...2006-09-09ajax添加數(shù)據(jù)后如何在網(wǎng)頁顯示
這篇文章主要介紹了ajax添加數(shù)據(jù)顯示在網(wǎng)頁上,此文是通過ajax獲取后臺json數(shù)據(jù)來實(shí)現(xiàn)此功能,需要的朋友可以參考下2015-07-07詳解ASP.NET提取多層嵌套json數(shù)據(jù)的方法
本篇文章主要介紹了ASP.NET提取多層嵌套json數(shù)據(jù)的方法,利用第三方類庫Newtonsoft.Json提取多層嵌套json數(shù)據(jù)的方法,有興趣的可以了解一下。2017-02-02