65 lines
2.2 KiB
C#
Executable File
65 lines
2.2 KiB
C#
Executable File
using System;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.StaticFiles;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.FileProviders;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
namespace ETFileServer
|
|
{
|
|
public class Startup
|
|
{
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
//services.AddDirectoryBrowser(); //开启目录浏览
|
|
services.AddControllers();
|
|
}
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
|
|
|
|
IConfigurationRoot config = new ConfigurationBuilder()
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile("appsettings.json", true)
|
|
.Build();
|
|
|
|
string configDir = config["DirectoryPath"];
|
|
|
|
configDir = new DirectoryInfo(configDir).FullName;
|
|
Console.WriteLine(configDir);
|
|
// UseStaticFiles(app, configDir);
|
|
app.UseRouting();
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
app.Run(context =>
|
|
{
|
|
return Task.CompletedTask;
|
|
} );
|
|
}
|
|
|
|
private void UseStaticFiles(IApplicationBuilder app, string filePath)
|
|
{
|
|
StaticFileOptions staticfile = new StaticFileOptions
|
|
{
|
|
ServeUnknownFileTypes = true,
|
|
FileProvider = new PhysicalFileProvider(filePath),
|
|
DefaultContentType = "application/x-msdownload"
|
|
};
|
|
// 设置MIME类型类型
|
|
staticfile.ContentTypeProvider = new FileExtensionContentTypeProvider { Mappings = { ["*"] = "application/x-msdownload" } };
|
|
//app.UseDirectoryBrowser(new DirectoryBrowserOptions() { FileProvider = staticfile.FileProvider });
|
|
app.UseStaticFiles(staticfile);
|
|
}
|
|
|
|
public static IConfigurationRoot config { get; set; }
|
|
}
|
|
} |