当前位置: 首页 > news >正文

ASP.NET Core 中的 静态文件

Static Files

Static Files 包括 HTML,CSS,图片,JavaScript,以及其他静态资源文件。
即网站本身的内容。

Static Files 服务

Static Files 保存在项目的 Web Root 目录,即 wwwroot 文件夹中。
而wwwroot目录是Content Root 的子目录, Content Root 还包括其他代码文件,.exe 和 .dll 文件。
wwwroot文件夹的结构如下:

  • css
  • js
  • lib
  • images
    • 1.jpg

在App启动时调用UseStaticFiles启用静态文件service后,会将wwwroot 目录映射到 https:///

app.UseStaticFiles();

可以通过url https://localhost:5001/images/1.jpg 访问这个图片文件。

映射其他目录

如果想将其他目录映射到 https:///otherroot/images/1.jpg,比如

  • wwwroot
    • css
    • images
    • js
  • otherroot
    • images
      • 1.jpg

可以这样映射:

app.UseStaticFiles(new StaticFileOptions
{FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "otherroot")),RequestPath = "/otherroot",OnPrepareResponse = ctx =>{var cacheMaxAgeOneWeek = (60 * 60 * 24 * 7).ToString();ctx.Context.Response.Headers.Append("Cache-Control", $"public, max-age={cacheMaxAgeOneWeek}"); //添加过期时间}
});

然后在html代码中可以这样引用:

<img src="~/otherroot/images/1.png" class="img" asp-append-version="true" alt="Test">

设置 Static Files 的访问权限

需要满足3个条件:

  • 不保存在wwwroot下
  • 先调用UseAuthorization,再调用UseStaticFiles
  • 设置 fallback authorization 策略
builder.Services.AddAuthorization(options =>
{options.FallbackPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
});

以禁止匿名访问这些文件。

列出文件目录

Directory Browsing 服务可以列出某个目录下的所有文件。
可以通过 AddDirectoryBrowser()启用:

builder.Services.AddDirectoryBrowser();
var fileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.WebRootPath, "MyImages"));
var requestPath = "/MyImages";
app.UseDirectoryBrowser(new DirectoryBrowserOptions
{FileProvider = fileProvider,RequestPath = requestPath
});

使用默认文件

使用app.UseDefaultFiles()后,

app.UseDefaultFiles();

会在wwwroot 下依次查找这些文件作为入口

  • default.htm
  • default.html
  • index.htm
  • index.html

也可以指定文件名mydefault.html:

var options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("mydefault.html");
app.UseDefaultFiles(options);

UseFileServer

app.UseFileServer 是 UseStaticFiles,UseDefaultFiles 或者 UseDirectoryBrowser的组合。

builder.Services.AddDirectoryBrowser();
app.UseFileServer(enableDirectoryBrowsing: true);

上面的代码会启用UseStaticFiles,UseDefaultFiles,和DirectoryBrowsing。

FileExtensionContentTypeProvider

FileExtensionContentTypeProvider 的 Mappings 属性可以将文件扩展名 映射到 MIME Content Types。

// Set up custom content types - associating file extension to MIME type
var provider = new FileExtensionContentTypeProvider();
// Add new mappings
provider.Mappings[".myapp"] = "application/x-msdownload";
provider.Mappings[".htm3"] = "text/html";
provider.Mappings[".image"] = "image/png";
// Replace an existing mapping
provider.Mappings[".rtf"] = "application/x-msdownload";
// Remove MP4 videos.
provider.Mappings.Remove(".mp4");
app.UseStaticFiles(new StaticFileOptions
{ContentTypeProvider = provider
});
http://www.lryc.cn/news/147323.html

相关文章:

  • 2023年天府杯——C 题:码头停靠问题
  • 集丰照明|汽车美容店设计,装修色彩灯光搭配方法
  • 性能提升3-4倍!贝壳基于Flink + OceanBase的实时维表服务
  • 取数组中每个元素的最高位
  • Docker一键部署Nacos
  • 【数学建模】-- 模糊综合评价
  • Java 数据库改了一个字段, 前端传值后端接收为null问题解决
  • lnmp架构-mysql1
  • Threadlocal在项目中的应用
  • 个性化定制你的AI助手,AI指令提示词专家
  • mongodb聚合排序的一个巨坑
  • 无涯教程-分类算法 - 随机森林
  • c#常见的排序算法
  • Redis 持久化和发布订阅
  • k8s使用ECK(2.4)形式部署elasticsearch+kibana-http协议
  • [maven]关于pom文件中的<relativePath>标签
  • 11. 网络模型保存与读取
  • 新SDK平台下载开源全志V853的SDK
  • 多图详解VSCode搭建Java开发环境
  • 基于JavaWeb和mysql实现网上书城前后端管理系统(源码+数据库+开题报告+论文+答辩技巧+项目功能文档说明+项目运行指导)
  • Swift创建单例
  • 问道管理:市盈率怎么计算?
  • Ansible File模块,Ansible File模块详解,文件管理的自动化利器
  • 记录http与mqtt的区别
  • 导入excel数据给前端Echarts实现中国地图-类似热力图可视化
  • 【MySQL系列】MySQL复合查询的学习 _ 多表查询 | 自连接 | 子查询 | 合并查询
  • 微信小程序使用本地图片在真机预览不显示的问题解决
  • Texlive2023与Texstudio2023卸载与安装(详细全程)
  • 塞浦路斯公司注册 塞浦路斯公司开户 塞浦路斯公司年审
  • XSS盲打练习(简单认识反射型、存储型XSS和cookie欺骗)