在這一章,我們將學(xué)習(xí)如何使用文件。幾乎每個(gè)web應(yīng)用程序都需要一個(gè)重要特性:能夠從文件系統(tǒng)提供文件(靜態(tài)文件)。
案例
現(xiàn)在讓我們通過一個(gè)簡單的示例來了解我們?cè)谖覀兊膽?yīng)用程序如何提供這些靜態(tài)文件。
在這里,我們想要向我們的 FirstAppDemo 應(yīng)用程序添加一個(gè)簡單的 HTML 文件,該 HTML 文件放在web 根 (wwwroot) 文件夾。在解決方案資源管理器中右鍵單擊wwwroot文件夾并選擇Add→新項(xiàng)。
在中間窗格中,選擇 HTML 頁面并稱之為 index.html,單擊添加按鈕。
你會(huì)看到一個(gè)簡單的index.html文件。讓我們?cè)谄渲刑砑右恍┖唵蔚奈谋竞蜆?biāo)題如下所示。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Welcome to ASP.NET Core</title> </head> <body> Hello, Wolrd! this message is from our first static HTML file. </body> </html>
當(dāng)您運(yùn)行應(yīng)用程序并在瀏覽器中輸入index.html時(shí),您將看到app.Run中間件將拋出一個(gè)異常,因?yàn)槟壳霸谖覀兊膽?yīng)用程序中什么都沒有。
現(xiàn)在我們的項(xiàng)目中沒有中間件會(huì)去找文件系統(tǒng)上的任何文件。
為了解決這個(gè)問題,通過在解決方案資源管理器中右鍵單擊您的項(xiàng)目并選擇管理NuGet包進(jìn)入到NuGet包管理器。
搜索 Microsoft.AspNet.StaticFiles,會(huì)找到靜態(tài)文件中間件。讓我們安裝此 nuget 程序包,現(xiàn)在我們可以在Configure方法中注冊(cè)中間件。
讓我們?cè)谙旅娴某绦蛑兴镜腃onfigure方法中添加 UseStaticFiles 中間件。
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; namespace FirstAppDemo { public class Startup { public Startup() { var builder = new ConfigurationBuilder() .AddJsonFile("AppSettings.json"); Configuration = builder.Build(); } public IConfiguration Configuration { get; set; } // 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 http://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) { app.UseIISPlatformHandler(); app.UseDeveloperExceptionPage(); app.UseRuntimeInfoPage(); app.UseStaticFiles(); app.Run(async (context) => { throw new System.Exception("Throw Exception"); var msg = Configuration["message"]; await context.Response.WriteAsync(msg); }); } // Entry point for the application. public static void Main(string[] args) => WebApplication.Run<Startup>(args); } }
除非你通過傳入一些不同的配置參數(shù)來覆蓋選項(xiàng),否則靜態(tài)文件會(huì)對(duì)于一個(gè)給定的請(qǐng)求看作是請(qǐng)求路徑。這個(gè)請(qǐng)求路徑是相對(duì)于文件系統(tǒng)。
讓我們保存Startup.cs文件并刷新瀏覽器。
你現(xiàn)在可以看到index.html文件。你放置在wwwroot文件夾下任何地方的任何JavaScript文件、CSS文件或者HTML文件,您都能夠在Asp.Net Core中直接當(dāng)靜態(tài)文件使用。
/ This method gets called by the runtime. // Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { app.UseIISPlatformHandler(); app.UseDeveloperExceptionPage(); app.UseRuntimeInfoPage(); app.UseDefaultFiles(); app.UseStaticFiles(); app.Run(async (context) => { var msg = Configuration["message"]; await context.Response.WriteAsync(msg); }); }
這段中間件將監(jiān)聽傳入的請(qǐng)求,如果請(qǐng)求是根目錄,就查看是否有匹配的默認(rèn)文件。
您可以覆蓋這個(gè)中間件的選項(xiàng)來告訴它如何匹配默認(rèn)文件,但index.html是默認(rèn)情況下的一個(gè)默認(rèn)的文件。
讓我們保存 Startup.cs 文件并將您的瀏覽器轉(zhuǎn)到 web 應(yīng)用程序的根目錄。
你現(xiàn)在可以看到index.html是默認(rèn)文件。你安裝中間件的順序是很重要的,因?yàn)槿绻銓seDefaultFiles放置在UseStaticFiles之后,你將可能不會(huì)得到相同的結(jié)果。
如果你想要使用UseDefaultFiles和UseStaticFiles中間件,你可以使用另一個(gè)中間件Microsoft.aspnet.staticfiles,它也是NuGet包,它是一個(gè)服務(wù)器中間件。這本質(zhì)上是以正確的順序包含了默認(rèn)文件和靜態(tài)文件。
// This method gets called by the runtime. // Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { app.UseIISPlatformHandler(); app.UseDeveloperExceptionPage(); app.UseRuntimeInfoPage(); app. UseFileServer(); app.Run(async (context) => { var msg = Configuration["message"]; await context.Response.WriteAsync(msg); }); }
讓我們?cè)僖淮伪4?Startup.cs 文件。一旦你刷新瀏覽器,你將看到相同的結(jié)果,如下面的屏幕快照所示。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com