《ASP.NET Core 高性能系列》静态文件中间件

1、概述

  静态文件(如 HTML、CSS、图片和 JavaScript等文件)是 Web程序直接提供给客户端的直接加载的文件。 较比于程序动态交互的代码而言,其实原理都同样(走Http协议),css

ASP.NET Core中须要进行一些配置才能提供这些文件。html

2、wwwroot

  静态文件存储在项目的 Web 程序的 {ContentRoot}/wwwroot目录下,但可经过 UseWebRoot 方法更改路径 。 前端

Web 应用程序项目的 wwwroot 文件夹中默认有多个文件夹 :后端

  • wwwroot
    • css
    • images
    • js

用于访问 images 子文件夹中的文件的 URI 格式为 http://<server_address>/images/<image_file_name> 。 例如, http://localhost:1189/images/banner.pngapi

  经过如下代码开启静态文件访问功能(设置 {ContentRoot}/wwwroot为默认的静态文件工做目录)浏览器

public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles();
}

3、设置指定目录为静态文件工做目录

public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles(); //   wwwroot 目录

    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "the_path_to_yours")),
        RequestPath = "/GiveAName"
    });
}

  注意访问自定义的静态文件路径发生变化:http://localhost:1189/GiveAName/images/banner.png缓存

4、给静态文件添加客户端缓存

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    var cachePeriod = env.Production() ? "60000" : "600";
    app.UseStaticFiles(new StaticFileOptions
    {
        OnPrepareResponse = ctx =>
        {
            // 记得下面的引用:
            // using Microsoft.AspNetCore.Http;
            ctx.Context.Response.Headers.Append("Cache-Control", $"public, max-age={cachePeriod}");
        }
    });
}

  如上,在生产环境中,咱们给静态文件添加了60000(ms)的缓存,即:在一分钟内,客户端都会从浏览器本地拿文件,注意此手法,直接更新静态文件一时间是拿不到最新文件的.安全

 

 

5、利用PhysicalFile方法让静态文件的访问能够进行鉴权

  静态文件中间件容许浏览器端访问由静态文件中间件提供的全部静态文件(包括 wwwroot 下的文件),咱们设想实际的一个应用场景,咱们上传了一个文件到指定目录,而这个文件只能当事人本身能够进行访问,那么如何进行权限验证呢?app

[Authorize]//身份验证
public IActionResult UsersOwnPictrue()
{
    var file = Path.Combine(Directory.GetCurrentDirectory(), 
                            "StaticFilesPath", "images", "my.svg");
    return PhysicalFile(file, "image/svg+xml");//返回静态文件
}

6、先后端分离开发中的使用

  先后端开发分离的开发模式中,前端本身负责前端的一切交互工做,不只如此还会在后端工做没有启动前本身构造数据,ASP.NET Core静态文件中间件,正好可和此开发前后端分离

模式进行衔接,此处省略一万字,有一点你们比较关心的问题:如何设置项目起始页,如何设置前端项目中的默认页

public void Configure(IApplicationBuilder app)
{
    app.UseDefaultFiles();//必须在  前调用 。  实际上用于重写 URL,不提供文件。 app.UseStaticFiles(); //这两步
}UseStaticFilesUseDefaultFilesUseDefaultFiles
使用 UseDefaultFiles 会使用如下这些文件做为整个项目的默认起始页:
default.htm
default.html
index.htm
index.html
public void Configure(IApplicationBuilder app)
{
    // 使用其余文件做为默认静态页面文件
    DefaultFilesOptions options = new DefaultFilesOptions();
    options.DefaultFileNames.Clear();
    options.DefaultFileNames.Add("其余wwwroot下的.html");
    app.UseDefaultFiles(options);
    app.UseStaticFiles();
}

 

app.UseFileServer();//可将上面两步何为一步

 

7、关于UseFileServer

  UseFileServer 整合了 UseStaticFiles、UseDefaultFiles 和 UseDirectoryBrowser(可选,若是启用须要services.AddDirectoryBrowser())的功能,

public void Configure(IApplicationBuilder app)
{
   
    app.UseFileServer(new FileServerOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "文件夹")),
        RequestPath = "/请求名",
        EnableDirectoryBrowsing = true
    });
}
请求链接 对应路径
http://<server_address>/请求名/images/file1.png 文件夹/images/file1.png
http://<server_address>/请求名/ 文件夹/default.html

8、开启目录浏览功能

  此功能一般不要开启,由于比较危险,经过目录浏览,Web 应用的用户可查看目录列表和指定目录中的文件。 出于安全考虑,调用 Startup.Configure 中的 UseDirectoryBrowser 方法来启用目录浏览:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDirectoryBrowser();
}
 app.UseDirectoryBrowser(new DirectoryBrowserOptions //开启对wwwroot/images的文件进行浏览的功能
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images")),
        RequestPath = "/MyImages"
    });

9、指定指定文件类型的MIME类型

public void Configure(IApplicationBuilder app)
{

    var provider = new FileExtensionContentTypeProvider();

    provider.Mappings[".rtf"] = "application/x-msdownload";
    // 移除指定文件的解析
    provider.Mappings.Remove(".mp4");
    ......
}
public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles(new StaticFileOptions
    {
       //未知类型的ContentType
        ServeUnknownFileTypes = true,
        DefaultContentType = "bala/your type"
    });
}
相关文章
相关标签/搜索