欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

NETCore2.0靜態(tài)文件目錄的相關(guān)知識總結(jié)-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)主營谷城網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,APP應(yīng)用開發(fā),谷城h5小程序制作搭建,谷城網(wǎng)站營銷推廣歡迎谷城等地區(qū)企業(yè)咨詢

概要:

本文通過示例,講解了 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è)置當(dāng)前目錄的內(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(); // 使用默認(rèn)文件夾 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ù)使當(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盤文件展示

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è)置默認(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處理。

運行效果

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

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

已處理的文件類型

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

這樣,我們就可以訪問任意類型的靜態(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盤中的任意目錄了,效果如下:

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è)置默認(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");
   });
  }
 }
}

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

標(biāo)題名稱:NETCore2.0靜態(tài)文件目錄的相關(guān)知識總結(jié)-創(chuàng)新互聯(lián)
路徑分享:http://aaarwkj.com/article30/phjso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、品牌網(wǎng)站設(shè)計、網(wǎng)站營銷、網(wǎng)站改版、網(wǎng)站內(nèi)鏈、網(wǎng)站維護

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
免费精品99久久久国产| 美国一级二级三级黄片| 中文岳妇荡欲丰满肥熟| 亚洲免费成人一区二区| 日本人妻久久中文字幕| 区二区三区毛片乱码免费| 欧美日韩国产福利在线观看| 国产毛毛片一区二区三区| 亚洲成人精品夫妻av| 变态另类专区一区二区三区| 久久精品国产亚洲av不丁香| 99热这里在线只有精品| 成人免费大片在线观看视频| 久久国产欧美日韩精品| 日日爱欧美精品亚洲成| 亚洲中少妇久久中文字幕| 国产女同互慰一区二区| 男人天堂av一区二区| 麻豆映画传媒在线播放| 中文字幕精品一区二区三| 久久中文字幕日韩精品| 日本午夜福利久久久| av在线中文字幕剧情| 97色伦综合在线欧美| 国产在线精品91系列| 国产无套内射三级视频| 特黄特色的日本大片| 全黄性性激高免费放视频| 开心久久婷婷综合中文字幕| 成人中文字幕日韩电影| 日韩亚洲一区在线观看| 国产精品乱码精品久久久| 美女被男人操国产精品| 日韩中文字幕久久中文字幕| 蜜桃网站视频免费观看 | 日本 午夜 在线 视频| 性生活免费在线观看视频| 国产精品国产三级国av中文| 亚洲一区二区三区av电影| 国产一区二区三区精品久| 久久国产麻豆精品电影|