Asp.NET WebAPI使用文件服务

2022/6/9 1:21:49

本文主要是介绍Asp.NET WebAPI使用文件服务,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文件服务

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使用文件服务的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程