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

c# .Net Core靜態(tài)文件服務(wù)器的新人入門教程

 更新時間:2018年07月04日 11:09:39   作者:MrITzhongzi  
這篇文章主要給大家介紹了關(guān)于c# .Net Core靜態(tài)文件服務(wù)器的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

概要:

本文通過示例,講解了 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à)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論