溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

NET Core2.0靜態(tài)文件目錄的相關(guān)知識總結(jié)

發(fā)布時間:2021-09-06 16:22:17 來源:億速云 閱讀:97 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“NET Core2.0靜態(tài)文件目錄的相關(guān)知識總結(jié)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“NET Core2.0靜態(tài)文件目錄的相關(guān)知識總結(jié)”吧!

概要:

本文通過示例,講解了 NET Core2.0 靜態(tài)文件目錄的相關(guān)知識,并附帶解析,適合新手,并附帶了完整的項目代碼。(項目通過 vs2017 初始化的 ASP.NET Core 應(yīng)用程序,之后選擇***空項目***)

示例代碼

項目結(jié)構(gòu)

NET Core2.0靜態(tài)文件目錄的相關(guān)知識總結(jié)

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è)置當前目錄的內(nèi)容
  .UseIISIntegration()
  .UseUrls("http://*:5000") // 使 項目在 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(); // 使用默認文件夾 wwwroot 僅僅shi wwwroot對外可見
  
  app.Run(async (context) => 
  {
  await context.Response.WriteAsync("hello jesus");
  });
 }
 }
}

運行效果:

NET Core2.0靜態(tài)文件目錄的相關(guān)知識總結(jié)

解析: 這是一個基本的靜態(tài)文件服務(wù)器app.UseStaticFiles() 函數(shù)使當前內(nèi)容目錄下默認的 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); // 使用默認文件夾 wwwroot 僅僅shi wwwroot對外可見
  
  app.Run(async (context) => 
  {
  await context.Response.WriteAsync("hello jesus");
  });
 }

c盤文件展示

NET Core2.0靜態(tài)文件目錄的相關(guān)知識總結(jié)

運行效果

NET Core2.0靜態(tài)文件目錄的相關(guān)知識總結(jié)

這樣我們就可以訪問任意目錄下的文件了,那么問題來了,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è)置默認 MIME TYPE
  var provider = new FileExtensionContentTypeProvider();
  provider.Mappings.Add(".log", "text/plain"); // 手動設(shè)置對應(yīng)的 MIME TYPE
  staticfile.ContentTypeProvider = provider;

  app.UseStaticFiles(staticfile); // 使用默認文件夾 wwwroot 僅僅shi wwwroot對外可見
  // 設(shè)置 指定目錄的文件 可以被訪問 end

  app.Run(async (context) =>
  {
  await context.Response.WriteAsync("hello jesus");
  });
 }

我們將不能識別的文件類型默認為 : "application/x-msdownload",即遇到我們沒處理的,不能識別的類型統(tǒng)統(tǒng)下載下來。

provider.Mappings.Add(".log", "text/plain"); // 手動設(shè)置對應(yīng)的 MIME TYPE 。我們手動增加了 對后綴為.log的文件類型的處理,當成文本文件處理,即txt處理。

運行效果

未知的文件 (我們訪問789.ggg文件,此文件類型我們未處理過)

NET Core2.0靜態(tài)文件目錄的相關(guān)知識總結(jié)

已處理的文件類型

NET Core2.0靜態(tài)文件目錄的相關(guān)知識總結(jié)

這樣,我們就可以訪問任意類型的靜態(tài)文件了,那么問題又來了, 我想訪問一個目錄下所有的文件,即訪問某個目錄怎么辦?

在 NET Core 中訪問目錄的功能默認是禁止的,需要手動開啟。

步驟:

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盤中的任意目錄了,效果如下:

NET Core2.0靜態(tài)文件目錄的相關(guān)知識總結(jié)

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è)置默認 MIME TYPE
    var provider = new FileExtensionContentTypeProvider();
    provider.Mappings.Add(".log", "text/plain"); // 手動設(shè)置對應(yīng)的 MIME TYPE
    staticfile.ContentTypeProvider = provider;

   app.UseStaticFiles(staticfile); // 使用默認文件夾 wwwroot 僅僅shi wwwroot對外可見
   // 設(shè)置 指定目錄的文件 可以被訪問 end

   app.Run(async (context) => 
   {
    await context.Response.WriteAsync("hello jesus");
   });
  }
 }
}

到此,相信大家對“NET Core2.0靜態(tài)文件目錄的相關(guān)知識總結(jié)”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI