在 Program.cs 文件中定义 UseStaticFiles
app.UseStaticFiles(new StaticFileOptions{
FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "Public/images")),
RequestPath = "/img"
});
复制代码
在 /Public/images
目录下有一张 golang.png
图片。在 html 中可以按照 /img/golang.png
的方式引用。/img/golang.png
映射回 http://xxx/Public/images/golang.png
。
Asp.Net Core 默认资源文件在 wwwroot下,如果需要在 html 中访问不在wwwroot目录下的资源文件,可以通过UseStaticFiles自定义资源路径。在 html 中 ~
别名指代 wwwroot
。
资源可以和权限结合起来。