• 欢迎光临~

Asp.NET WebAPI使用文件服务

开发技术 开发技术 2022-06-08 次浏览

文件服务

using Microsoft.Extensions.FileProviders;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddControllersWithViews();

builder.Services.AddDirectoryBrowser();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseFileServer(new FileServerOptions
{
    FileProvider = new PhysicalFileProvider(
           Path.Combine(builder.Environment.ContentRootPath, "MyStaticFiles")),
    RequestPath = "/StaticFiles",
    EnableDirectoryBrowsing = true
});

app.UseAuthorization();

app.MapDefaultControllerRoute();
app.MapRazorPages();

app.Run();
  • 浏览 https://localhost:7160/StaticFiles/ ,返回文件列表

Examples

https://github.com/huzuohuyou/DotNet6-Examples/tree/main/UseStaticFiles

Asp.NET WebAPI使用文件服务

程序员灯塔
转载请注明原文链接:Asp.NET WebAPI使用文件服务
喜欢 (0)