欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET Core中的靜態(tài)文件介紹

 更新時(shí)間:2022年04月13日 10:33:22   作者:Ruby_Lu  
這篇文章介紹了ASP.NET Core中的靜態(tài)文件,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

靜態(tài)文件(HTML,CSS,圖片和Javascript之類(lèi)的資源)會(huì)被ASP.NET Core應(yīng)用直接提供給客戶(hù)端。

靜態(tài)文件通常位于網(wǎng)站根目錄(web root) <content-root>/wwwroot文件夾下。通常會(huì)把項(xiàng)目的當(dāng)前目錄設(shè)置為Content root,這樣項(xiàng)目的web root就可以在開(kāi)發(fā)階段被明確。

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseContentRoot(Directory.GetCurrentDirectory()) //設(shè)置當(dāng)前目錄
                .UseStartup<Startup>();

靜態(tài)文件能夠被保存在網(wǎng)站根目錄下的任意文件夾內(nèi),并通過(guò)相對(duì)根的路徑來(lái)訪(fǎng)問(wèn)。使用vs創(chuàng)建一個(gè)默認(rèn)的Web應(yīng)用程序時(shí),在wwwroot目錄下會(huì)生成幾個(gè)文件夾:css,images,js。如果壓迫訪(fǎng)問(wèn)images目錄下的圖片:

http://<app>/iamges/filename

https://localhost:44303/iamges/filename

要想使用靜態(tài)文件服務(wù),必須配置中間件,把靜態(tài)文件中間件加入到管道。靜態(tài)文件一般會(huì)默認(rèn)配置,在Configure方法中調(diào)用app.UseStaticFiles()。

app.UseStaticFiles() 使得web root(默認(rèn)為wwwroot)下的文件可以被訪(fǎng)問(wèn)。同時(shí)可以通過(guò)UseStaticFiles方法將其他目錄下的內(nèi)容也可以向外提供:

假如wwwroot外面有一個(gè)MyStaticFiles文件夾,要訪(fǎng)問(wèn)文件夾里面的資源test.png:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseStaticFiles(new StaticFileOptions() {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")), //用于定位資源的文件系統(tǒng)
                RequestPath = new PathString("/StaticFiles") //請(qǐng)求地址
            });

        }

可以通過(guò)訪(fǎng)問(wèn)

http://<app>/StaticFiles/test.png

https://localhost:44303/StaticFiles/test.png

1.靜態(tài)文件授權(quán)

靜態(tài)文件組件默認(rèn)不提供授權(quán)檢查。任何通過(guò)靜態(tài)文件中間件訪(fǎng)問(wèn)的文件都是公開(kāi)的。要想給文件授權(quán),可以將文件保存在wwwroot之外,并將目錄設(shè)置為可被靜態(tài)文件中間件能夠訪(fǎng)問(wèn),同時(shí)通過(guò)一個(gè)controller action來(lái)訪(fǎng)問(wèn)文件,在action中授權(quán)后返回FileResult

2.目錄瀏覽

目錄瀏覽允許網(wǎng)站用戶(hù)看到指定目錄下的目錄和文件列表?;诎踩紤],默認(rèn)情況下是禁止目錄訪(fǎng)問(wèn)功能。在Startup.Configure中調(diào)用UseDirectoryBrowser擴(kuò)展方法可以開(kāi)啟網(wǎng)絡(luò)應(yīng)用目錄瀏覽:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseStaticFiles();

            app.UseDirectoryBrowser(new DirectoryBrowserOptions() {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(),@"wwwroot\images")),
                RequestPath = new PathString("/MyImages") //如果不指定RequestPath,會(huì)將PhysicalFileProvider中的路徑參數(shù)作為默認(rèn)文件夾,替換掉wwwroot
}); }

然后在Startup.CongigureServices中調(diào)用AddDirectoryBrowser擴(kuò)展方法。

這樣就可以通過(guò)訪(fǎng)問(wèn)http://<app>/MyImages瀏覽wwwroot/images文件夾中的目錄,但是不能訪(fǎng)問(wèn)文件:

要想訪(fǎng)問(wèn)具體文件需要調(diào)用UseStaticFiles配置:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseStaticFiles();
            app.UseStaticFiles(new StaticFileOptions() {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images")), //用于定位資源的文件系統(tǒng)
                RequestPath = new PathString("/MyImages")
            });
            app.UseDirectoryBrowser(new DirectoryBrowserOptions() {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(),@"wwwroot\images")),
                RequestPath = new PathString("/MyImages")
            });

        }

3.默認(rèn)文件

設(shè)置默認(rèn)首頁(yè)能給站點(diǎn)的訪(fǎng)問(wèn)者提供一個(gè)起始頁(yè),在Startup.Configure中調(diào)用UseDefaFiles擴(kuò)展方法:

app.UseDefaultFiles(options);
            app.UseStaticFiles();

UseDefaultFiles必須在UseStaticFiles之前調(diào)用。UseDefaultFiles只是重寫(xiě)了URL,而不是真的提供了一個(gè)這樣的文件,瀏覽器URL將繼續(xù)顯示用戶(hù)輸入的URL。所以必須開(kāi)啟靜態(tài)文件中間件。而且默認(rèn)文件必須放在靜態(tài)文件中間件可以訪(fǎng)問(wèn)得到的地方,默認(rèn)是wwwroot中。

通過(guò)UseDefaultFiles,請(qǐng)求文件夾的時(shí)候檢索以下文件:

default.htm

default.html

index.htm

index.html

也可以使用UseDefaultFiles將默認(rèn)頁(yè)面改為其他頁(yè)面:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            DefaultFilesOptions options = new DefaultFilesOptions();
            options.DefaultFileNames.Clear();
            options.DefaultFileNames.Add("mydefault.html");
            app.UseDefaultFiles(options);
            app.UseStaticFiles();


            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

4.UseFileServer

UseFileServer集合了UseStaticFiles,UseDefaultFiles,UseDirectoryBrowser。

調(diào)用app.UseFileServer(); 請(qǐng)用了靜態(tài)文件和默認(rèn)文件,但不允許直接訪(fǎng)問(wèn)目錄。需要調(diào)用app.UseFileServer(enableDirectoryBrowsing:true); 才能啟用目錄瀏覽功能。

如果想要訪(fǎng)問(wèn)wwwroot以外的文件,需要配置一個(gè)FileServerOptions對(duì)象

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
app.UseStaticFiles();//如果不調(diào)用,將不會(huì)啟動(dòng)默認(rèn)功能。
            app.UseFileServer(new FileServerOptions()
            {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")),
                RequestPath = new PathString("/StaticFiles"),
                EnableDirectoryBrowsing = true
            });
}

注意,如果將enableDirectoryBrowsing設(shè)置為true,需要在ConfigureServices中調(diào)用services.AddDirectoryBrowser();

如果默認(rèn)文件夾下有默認(rèn)頁(yè)面,將顯示默認(rèn)頁(yè)面,而不是目錄列表。

5.FileExtensionContentTypeProvider

FileExtensionContentTypeProvider類(lèi)包含一個(gè)將文件擴(kuò)展名映射到MIME內(nèi)容類(lèi)型的集合。

例如:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            var provider = new FileExtensionContentTypeProvider();
            provider.Mappings[".htm3"] = "text/html";
            provider.Mappings["images"] = "iamge/png";
            provider.Mappings.Remove(".mp4");

            app.UseStaticFiles(new StaticFileOptions() {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")),
                RequestPath = new PathString("/StaticFiles"),
                ContentTypeProvider = provider
            });
}

更多MIME類(lèi)型可以訪(fǎng)問(wèn):http://www.iana.org/assignments/media-types/media-types.xhtml

6.非標(biāo)準(zhǔn)的內(nèi)容類(lèi)型

如果用戶(hù)請(qǐng)求了一個(gè)未知的文件類(lèi)型,靜態(tài)文件中間件將會(huì)返回HTTP 404響應(yīng)。如果啟用目錄瀏覽,則該文件的鏈接將會(huì)被顯示,但RUI會(huì)返回一個(gè)HTTP404錯(cuò)誤。

使用UseStaticFiles方法可以將未知類(lèi)型作為指定類(lèi)型處理:

app.UseStaticFiles(new StaticFileOptions() {
                ServeUnknownFileTypes = true,
                DefaultContentType = "application/x-msdownload"
            });

對(duì)于未識(shí)別的,默認(rèn)為application/x-msdownload,瀏覽器將會(huì)下載這些文件。

到此這篇關(guān)于ASP.NET Core靜態(tài)文件的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論