zoukankan      html  css  js  c++  java
  • net开发学习:目录浏览

    目录浏览允许在指定目录中列出目录。出于安全考虑,目录浏览默认处于禁用状态。

    通过以下方式启用目录浏览:

    注册目录浏览服务:Startup.ConfigureServices 中的 AddDirectoryBrowser。
    目录浏览设定:Startup.Configure 中的 UseDirectoryBrowser。

    新增空白web模板

    dotnet new web

    代码实现:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDirectoryBrowser(); //不注册目录浏览服务似乎也不影响功能实现
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
          //using System.IO;
          //using Microsoft.Extensions.FileProviders;
          app.UseStaticFiles(new StaticFileOptions
          {
              FileProvider = new PhysicalFileProvider(
                   Path.Combine(env.WebRootPath,"images")),
              RequestPath = "/MyImages"
          });
    
          app.UseDirectoryBrowser(new DirectoryBrowserOptions
          {
              FileProvider = new PhysicalFileProvider(
                  Path.Combine(env.WebRootPath, "images")),
              RequestPath = "/MyImages"
          });
    }
    

    错误情况:

    • Project 中缺少 env.WebRootPath 指向的 wwwroot 目录
    Application startup exception
          System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
             at System.IO.Path.Combine(String path1, String path2)
    
    • wwwroot 目录下缺少 images 目录
    Application startup exception
          System.IO.DirectoryNotFoundException: E:	estwwwrootimages
             at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
             at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root)
    
    • 缺少 app.UseStaticFiles 设定,会无法访问具体的静态文件

    效果演示:

    上述代码允许使用 URL http://localhost:5000/MyImages 浏览 wwwroot/images 文件夹的目录,并链接到每个文件和文件夹:

    参考来源:ASP.NET Core 中的静态文件:目录浏览

  • 相关阅读:
    HDU 1501 Zipper(DFS)
    HDU 2181 哈密顿绕行世界问题(DFS)
    HDU 1254 推箱子(BFS)
    HDU 1045 Fire Net (DFS)
    HDU 2212 DFS
    HDU 1241Oil Deposits (DFS)
    HDU 1312 Red and Black (DFS)
    HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
    HDU 1022 Train Problem I(栈)
    HDU 1008 u Calculate e
  • 原文地址:https://www.cnblogs.com/tianliupingzong/p/14729027.html
Copyright © 2011-2022 走看看