要在ASP.NET Core中访问Inetpub之外的文件夹,您可以使用物理文件提供程序。以下是一个示例解决方案:
using Microsoft.Extensions.FileProviders;
using System.IO;
public void ConfigureServices(IServiceCollection services)
{
// 添加物理文件提供程序
services.AddSingleton(new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "YourFolderPath")));
// ...
}
请将"YourFolderPath"替换为您要访问的文件夹的路径。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...
// 启用静态文件中间件
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "YourFolderPath")),
RequestPath = "/YourRequestPath" // 设置URL路径以访问文件夹中的文件
});
// ...
}
请将"YourFolderPath"替换为您要访问的文件夹的路径,并将"YourRequestPath"替换为您要在URL中使用的路径。
现在,您就可以在ASP.NET Core应用程序中访问Inetpub之外的文件夹了。例如,如果您的文件夹路径为"C:\YourFolder",并且您在URL中使用的路径为"/files",则可以通过访问"http://localhost:5000/files/yourfile.txt"来访问文件夹中的"yourfile.txt"文件。