加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

asp .net core静态文件资源的深入讲解

发布时间:2020-09-20 00:20:02 所属栏目:编程 来源:网络整理
导读:这篇文章首要给各人先容了关于asp .net core静态文件资源的相干资料,文中通过示例代码先容的很是具体,对各人的进修可能事变具有必然的参考进修代价,必要的朋
副问题[/!--empirenews.page--]

对静态资源的简朴的一个轮廓,在《从头清算.net core 计1400篇》系列后头会深入。

正文

我们在插手中间件是这样写的:

app.UseStaticFiles();

默认是给wwwroot提供资源。

那么我会见https://localhost:44330/js/site.js 资源,就可以会见到。

// Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification // for details on configuring this project to bundle and minify static web assets. // Write your JavaScript code.

同样我们可以自界说路径。

app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Static")), RequestPath="/static" });

上面在根目次下的static成立路由,路由路径static 为标识。

会见:

https://localhost:44330/static/images/index.jpg

就能看到一张图片了。

同样再次会见,https://localhost:44330/js/site.js 依然可以会见,看了这个wwwroot 是一个钉子户,无论怎样添加照旧存在的。

const string cacheMaxAge = "60480"; app.UseHttpsRedirection(); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Static")), RequestPath="/static", OnPrepareResponse = ctx => { ctx.Context.Response.Headers.Append("cache-control", $"public,max-age={cacheMaxAge}"); } } );

可以配置一些缓存。

静态文件授权

官方倒是提供了两种要领。

一种是,让静态文件路由放到权限之后。

app.UseAuthentication(); app.UseAuthorization(); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider( Path.Combine(env.ContentRootPath, "Static")), RequestPath = "/static" });

另一种较量自界说高:

[Authorize] public IActionResult BannerImage() { var filePath = Path.Combine( _env.ContentRootPath, "MyStaticFiles", "images", "red-rose.jpg"); return PhysicalFile(filePath, "image/jpeg"); }

可以按照参数做一些逻辑变革。

可是这些方法较量影响机能,一样平常来说静态文件是开放的,而用户上传的文件是通过加密的,放在存储处事器上。

虽然小型项目,可以用用。

静态文件目次

Configure中添加:

app.UseDirectoryBrowser(new DirectoryBrowserOptions { FileProvider=new PhysicalFileProvider(Path.Combine(env.ContentRootPath,"Static")), RequestPath="/static" });

这此中间件注入的位置是应该在UseRouting之前的,同样是机能题目。

然后在ConfigureServices中添加:

services.AddDirectoryBrowser();

结果:

这种方法呢,一样平常只是在dev情形下打开,真正的出产情形因为安详题目就不打开的。

默认文档

app.UseDefaultFiles(); app.UseStaticFiles();

app.UseStaticFiles(); 才是真正的路由。

app.UseDefaultFiles(); 只是说提供一些参数,好比设置下面这些为默认项。

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

着实是这样一个进程,app.UseStaticFiles() 假如没有找到响应的路由,那么应该给下一此中间件。

假如挪用了app.UseDefaultFiles(),那么会去找是否存在默认项,默认是去wwwroot 下探求上述的默认项。

默认文档可以举办修改:

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

UseFileServer 团结了 UseStaticFiles、UseDefaultFiles 和 UseDirectoryBrowser(可选)的成果。

app.UseFileServer(enableDirectoryBrowsing: true);

enableDirectoryBrowsing 暗示是否行使UseDirectoryBrowser。

FileExtensionContentTypeProvider

FileExtensionContentTypeProvider 类包括 Mappings 属性,用作文件扩展名到 MIME 内容范例的映射。

好比说我去会见:https://localhost:44330/static/test.myapp

我在static 下有test.mapp 这个文件,可是静态文件处理赏罚并没有行止理赏罚。

缘故起因:

客服端发了这样一个哀求,人家接管这些流,可是处事器找到到,myapp 对应的媒体范例,那么这个时辰客户端就不会接管了,处事端也以为没有找到。

官方给了例子:

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.UseDefaultFiles(); app.UseStaticFiles(); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Static")), RequestPath = "/static", OnPrepareResponse = ctx => { ctx.Context.Response.Headers.Append("cache-control", $"public,max-age={cacheMaxAge}"); }, ContentTypeProvider= provider }

给他加一个媒体范例,以为myapp 应该是一个必要下载文件。

然后运行之,然后就会呈现下载。

同样,我们写的是.html,假如我们不喜好可以去写.htm3也行。

https://localhost:44330/static/index.htm3

功效:

由于provider.Mappings[".htm3"] = "text/html"; ,.htm3被映射成了text/html,那么客户端就凭证这种名目处理赏罚。以是模板引擎就可以多样性,有乐趣本身也可以去计划。

这就是媒体范例映射。

假如是媒体范例未知的环境下,那么可以这样:

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读