概要:本文通過示例,講解了 NET Core2.0 靜態檔案目錄的相關知識,並附帶解析,適合新手,並附帶了完整的專案程式碼。(專案通過 vs2017 初始化的 ASP.NET Core 應用程式,之後選擇***空專案***)
示例程式碼
專案結構
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()) // 設定當前目錄的內容
.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");
});
}
}
}
複製程式碼
// 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");
});
}
複製程式碼
// 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盤,也可以指定其他目錄
// 設定 對應的檔案型別(防止Mime type沒事別出來,打不開或出現404錯誤)
staticfile.ServeUnknownFileTypes = true;
staticfile.DefaultContentType = "application/x-msdownload";// 設定預設 MIME TYPE
var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".log", "text/plain"); // 手動設定對應的 MIME TYPE
staticfile.ContentTypeProvider = provider;
app.UseStaticFiles(staticfile); // 使用預設資料夾 wwwroot 僅僅shi wwwroot對外可見
// 設定 指定目錄的檔案 可以被訪問 end
app.Run(async (context) =>
{
await context.Response.WriteAsync("hello jesus");
});
}
複製程式碼
// 設定 目錄可瀏覽 start
var dir = new DirectoryBrowserOptions();
dir.FileProvider = new PhysicalFileProvider(@"C:\");
app.UseDirectoryBrowser(dir);
// 設定 目錄可瀏覽 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)
{
// 設定 目錄可瀏覽 start
var dir = new DirectoryBrowserOptions();
dir.FileProvider = new PhysicalFileProvider(@"C:\");
app.UseDirectoryBrowser(dir);
// 設定 目錄可瀏覽 end
// 設定 指定目錄的檔案 可以被訪問 start
var staticfile = new StaticFileOptions();
staticfile.FileProvider = new PhysicalFileProvider(@"C:\"); // 指定目錄,這裡指的是C盤,也可以指定其他目錄
// 設定 對應的檔案型別(防止Mime type沒事別出來,打不開或出現404錯誤)
staticfile.ServeUnknownFileTypes = true;
staticfile.DefaultContentType = "application/x-msdownload";// 設定預設 MIME TYPE
var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".log", "text/plain"); // 手動設定對應的 MIME TYPE
staticfile.ContentTypeProvider = provider;
app.UseStaticFiles(staticfile); // 使用預設資料夾 wwwroot 僅僅shi wwwroot對外可見
// 設定 指定目錄的檔案 可以被訪問 end
app.Run(async (context) =>
{
await context.Response.WriteAsync("hello jesus");
});
}
}
}
複製程式碼