ASP.NET Core 添加静态目录访问、使其它目录可被访问

内容纲要

使用app.UseFileServer

在 public void Configure(){}中,修改或添加

1 app.UseFileServer(new FileServerOptions()
2             {
3                 FileProvider =new PhysicalFileProvider
4                 (
5                     Path.Combine(Directory.GetCurrentDirectory(),@"StaticFile/Image")),   //实际目录地址
6 RequestPath=new Microsoft.AspNetCore.Http.PathString("/Image"), //用户访问地址
7 EnableDirectoryBrowsing=true //开启目录浏览 9 });

EnableDirectoryBrowsing=true  表示是否开启目录浏览,当为true时,访问该目录,会列出文件列表,如图

《ASP.NET Core 添加静态目录访问、使其它目录可被访问》

 

这样用户访问http://url/Image时,实际访问的是 StaticFile/Image。

app.UseFileServer包含了
app.UseStaticFiles //静态文件访问
app.UseDefaultFiles //默认文件设置
app.UseDirectoryBrowsing //开启目录浏览

亦可使用 app.UseStaticFiles,在public void Configure(){}中修改或添加

1  app.UseStaticFiles(new StaticFileOptions()
2             {
3                  FileProvider = new PhysicalFileProvider
4                 (
5 Path.Combine(Directory.GetCurrentDirectory(),@"StaticFile/Image")), //实际目录地址
6 RequestPath=new Microsoft.AspNetCore.Http.PathString("/Image"), //用户访问地址
7 });

 

点赞
  1. purchase paxil说道:

    I am commenting to let you know of the fine encounter my cousin's daughter experienced reading your site. She even learned many issues, not to mention what it's like to have a wonderful giving mindset to have many people quite simply grasp chosen hard to do subject areas. You really exceeded our expectations. Many thanks for giving the insightful, healthy, educational and as well as easy thoughts on the topic to Jane.

  2. CITALOPRAM说道:

    My wife and i have been so peaceful when Michael managed to finish up his researching via the precious recommendations he obtained using your web site. It is now and again perplexing to simply happen to be making a gift of techniques which often some others might have been trying to sell. We recognize we need you to thank for this. The type of illustrations you made, the straightforward website navigation, the relationships your site make it easier to instill - it is most spectacular, and it is letting our son in addition to us reckon that this subject matter is interesting, which is certainly tremendously serious. Thanks for the whole lot!

CITALOPRAM进行回复 取消回复

邮箱地址不会被公开。 必填项已用*标注

You must enable javascript to see captcha here!