ASP.NET Core 2 学习笔记(五)静态文件
2018-05-25 11:02
1101 查看
之前的ASP.NET网站,只要把
*.html、
*.css、
*.jpg、
*.png、
*.js等静态文件放在项目根目录,默认都可以直接被浏览;但ASP.NET Core 小改了浏览静态文件的方式,默认根目录不再能浏览静态文件,需要指定静态文件的目录,才可以被浏览。
本篇将介绍ASP.NET Core浏览静态文件的方法。
试着在项目根目录及wwwroot目录中加入静态文件,例如:
项目根目录\index.html
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>MyWebsite</title> </head> <body> 项目根目录的 index.html </body> </html>
项目根目录\wwwroot\index.html
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>MyWebsite</title> </head> <body> wwwroot目录的 index.html </body> </html>
然后在网址栏输入:
http://localhost:5000/index.html
http://localhost:5000/wwwroot/index.html
会发现以上两个链接都没有办法打开index.html。
浏览静态文件,需要
Microsoft.AspNetCore.StaticFiles中间件,ASP.NET Core 2.0以上版本默认包含。
启用静态文件
在Startup.cs的
Configure对
IApplicationBuilder使用
UseStaticFiles方法注册静态文件的Middleware:
Startup.cs
// ... public class Startup { public void Configure(IApplicationBuilder app) { app.UseStaticFiles(); // ... app.Run(async context => { await context.Response.WriteAsync("Hello World! \r\n"); }); } }
UseStaticFiles默认启用静态文件的目录是wwwroot,设定完成后再次尝试开启URL:
http://localhost:5000/index.html
开启的内容会是:wwwroot目录的index.html。http://localhost:5000/wwwroot/index.html
依然无法显示静态文件。
UseStaticFiles注册的顺序可以在外层一点,比较不会经过太多不必要的Middleware。如图:
当Requset的URL文件不存在,则会转向到
Run的事件(如灰色箭头)。
变更网站目录
默认网站目录是wwwroot,如果想要变更此目录,可以在Program.cs的WebHost Builder用
UseWebRoot设置网站默认目录。
例如:把默认网站目录wwwroot改为public,如下:
using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; namespace MyWebsite { public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseWebRoot("public") .UseStartup<Startup>() .Build(); } }
启用指定目录
由于
UseStaticFiles只能拿到默认文件夹底下的文件,某些情况会需要特定目录也能使用静态文件。
例如:用npm安装的第三方库都放在项目目录底下的node_modules。
Startup.cs
// ... public class Startup { public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseStaticFiles(); app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(env.ContentRootPath, @"node_modules")), RequestPath = new PathString("/third-party") }); // ... } }
以上设定就会把URL
http://localhost:5000/third-party/example.js指向到项目目录\node_modules\example.js。
默认文件
比较友好的用户体验会希望
http://localhost:5000/可以自动指向到index.html。
能通过
UseDefaultFiles设定静态文件目录的默认文件。
Startup.cs
// ... public class Startup { public void Configure(IApplicationBuilder app) { app.UseDefaultFiles(); app.UseStaticFiles(); // ... } }
UseDefaultFiles
的职责是尝试请求默认文件。UseStaticFiles
的职责是回传请求的文件。
UseDefaultFiles必须注册在UseStaticFiles之前。
如果先注册UseStaticFiles,当URL是/时,UseStaticFiles找不到该文件,就会直接回传找不到;所以就没有机会进到UseDefaultFiles。
自定义默认文件
UseDefaultFiles的默认文件如下:
- default.htm
- default.html
- index.htm
- index.html
如果默认文件的文件名不在上列清单,也可以自定义要用什么名称当作默认文件。
通过DefaultFilesOptions设定后,传入
UseDefaultFiles:
Startup.cs
// ... public class Startup { public void Configure(IApplicationBuilder app) { var defaultFilesOptions = new DefaultFilesOptions(); defaultFilesOptions.DefaultFileNames.Add("custom.html"); app.UseDefaultFiles(defaultFilesOptions); app.UseStaticFiles(); // ... } }
文件清单
基本上为了网站安全性考量,不应该让使用者浏览服务器上面的文件清单,但如果真有需求要让使用者浏览文件清单也不是不行。
在Startup.cs的
Configure对
IApplicationBuilder使用
UseFileServer方法注册文件服务器的功能:
Startup.cs
// ... public class Startup { // ... public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseFileServer(new FileServerOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(env.ContentRootPath, @"bin") ), RequestPath = new PathString("/StaticFiles"), EnableDirectoryBrowsing = true }); } }
当打开
http://localhost:5000/StaticFiles时,就指向到项目目录\bin\目录,并且可以直接浏览文件目录及文件内容,如下:
参考
Working with static files in ASP.NET Core
老司机发车啦:https://github.com/SnailDev/SnailDev.NETCore2Learning
相关文章推荐
- [dotnetCore2.0]学习笔记之二: ASP.NET Core中,如何灵活使用静态文件和加载自定义配置
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- Asp.net core 学习笔记 ( Area and Feature folder structure 文件结构 )
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
- ASP.NET Core 2.0系列学习笔记-配置文件
- ASP.NET Core 2.0系列学习笔记-NLog日志配置文件
- Asp.net core 学习笔记 ( upload/download files 文件上传与下载 )
- 细说ASP.NET Core静态文件的缓存方式
- 细说ASP.NET Core静态文件的缓存方式
- ASP.NET Core 中文文档 第三章 原理(3)静态文件处理
- ASP.NET Core 1.0 静态文件、路由、自定义中间件、身份验证简介
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(六)-- 依赖注入
- ASP.NET Core应用针对静态文件请求的处理[2]: 条件请求与区间请求
- ASP.NET Core 静态文件及JS包管理器(npm, Bower)的使用
- 解析如何利用一个ASP.NET Core应用来发布静态文件
- Asp.Net Core + Dapper + Repository 模式 + TDD 学习笔记
- 细说ASP.NET Core静态文件的缓存方式
- ASP.NET Core 1.0基础之静态文件处理
- [译]【NetCore学习笔记之Anchor TagHelper】ASP.NET Core MVC Anchor Tag Helper
- 细说ASP.NET Core静态文件的缓存方式