ASP.NET Core 1.0基础之静态文件处理
2016-01-22 11:27
1136 查看
来源
这些HTML , CSS files, image files, 和JavaScript这些静态文件,是ASP.NET能够直接响应给客户端的。本文详述下ASP.NET和静态文件的关系。
静态文件可以存储在任何位置,可以通过相对路径来获取。webroot下的静态文件可以通过http:///images/路径来直接获取。
为了能够提供静态文件,必须配置中间件来将静态文件添加到管道中。可以通过在Startup类中的Configre方法里调用UseStaticFiles扩展方法来实现。如下
如果你想提供webroot之外的静态文件,项目结构如下:
wwwroot
css
img
js
MyStaticFiles
test.png
为了使用户能够访问test.png文件,必须如下配置静态文件:
这时如果用户访问http:///StaticFiles/test.png这个路径,就可以获得对应文件。
浏览项目的images文件夹,结果如下:
如果有个文件夹在webroot之外,但是需要用户可浏览。项目结构如下:
wwwroot
css
img
js
MyStaticFiles
为了使用户可浏览MyStaticFiles目录,需要如下配置
这时,如果如果用户访问地址http:///StaticFiles,就能浏览MyStaticFiles文件夹中的文件。
如果你调用了UseDefaultFiles方法,如果用户输入了某个文件的路径,中间件会按顺序搜索如下文件。如果找到了,文件就会返回,就如同用户输入了完整路径一样。
default.htm
default.html
index.htm
index.html
为了指定其他默认文件,需要实例化DefaultFilesOptions对象,并设置其参数DefaultFileNames列表. 然后调用UseDefaultFiles,并把需要实例化DefaultFilesOptions对象对象传给它。下例,移除了DefaultFileNames列表中的默认的文件,并把mydefault.html添加作为唯一的默认文件。
此时,如果你浏览webroot中的文件夹,并携带文件名mydefault.html,该文件就可以获取,就如同用户输入了全路径。
但是如果你想设置的默认文件在webroot之外,你需要调用UseStaticFiles和UseDefaultFiles,并传给他们相对的参数值。然而更方便和推荐的方法是使用
UseFileServer方法。
如同UseStaticFiles, UseDefaultFiles和UseDirectoryBrowser,如果想服务webroot之外的文件,需要实例化和配置一个“options”对象,并作为参数传给UseFileServer。比如,有如下结构
wwwroot
css
img
js
MyStaticFiles
test.png
default.html
如上结构,如果想提供静态文件,默认文件以及目录浏览功能,如下只需调用UseFileServer方法。
如上例,如果用户访问如下路径
http:///StaticFiles/test.png - MyStaticFiles/test.png 文件会提供给浏览器
http:///StaticFiles - 因为有默认文件default.html,该文件会提供给浏览器。如果不存在默认文件,浏览器会提供文件浏览(因为 FileServerOptions.EnableDirectoryBrowsing属性设置为了true).
假设如下目录
wwwroot
css
js
images
test.image
按如下配置
如果访问http:///images这个目录,服务器会列出包括test.image的文件列表。但是如果尝试打开该文件,则会抛出错误。为了服务位置类型的文件,你需要将StaticFileOptions.ServeUnknownFileTypes熟悉nag设置为true并指定默认文件类型。如下
这是如果浏览器访问未知类型的文件,服务器会将他们作为image/png类型来对待。
目前为止,在处理为未知类型文件时,已经指定了默认文件类型。但是如果有多种未知文件类型呢。这就是FileExtensionContentTypeProvider类发挥作用的时候了。这个类内部有一个集合,来映射文件扩展名和MIME文件类型。为了自定义文件类型,只需实例化FileExtensionContentTypeProvider,并添加映射到FileExtensionContentTypeProvider.Mappings字典。如下例,添加了扩展.myapp到application/x-msdownload MIME类型。
现在如果用户尝试浏览.myapp后缀名的文件,用户会被提示下载该文件。
这些HTML , CSS files, image files, 和JavaScript这些静态文件,是ASP.NET能够直接响应给客户端的。本文详述下ASP.NET和静态文件的关系。
Serving static files
静态文件存储在项目的webroot下,webroot的位置可以通过project.json文件来配置。"webroot": "wwwroot"
静态文件可以存储在任何位置,可以通过相对路径来获取。webroot下的静态文件可以通过http:///images/路径来直接获取。
为了能够提供静态文件,必须配置中间件来将静态文件添加到管道中。可以通过在Startup类中的Configre方法里调用UseStaticFiles扩展方法来实现。如下
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... // Add static files to the request pipeline. app.UseStaticFiles(); ...
如果你想提供webroot之外的静态文件,项目结构如下:
wwwroot
css
img
js
MyStaticFiles
test.png
为了使用户能够访问test.png文件,必须如下配置静态文件:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... // Add MyStaticFiles static files to the request pipeline. app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider(@"D:\Source\WebApplication1\src\WebApplication1\MyStaticFiles"), RequestPath = new PathString("/StaticFiles") }); ...
这时如果用户访问http:///StaticFiles/test.png这个路径,就可以获得对应文件。
文件目录浏览
默认情况下,该功能是不可用的。开启该功能,需要在Startup.Configure中调用UseDirectoryBrowser方法,如下public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... // Turn on directory browsing for the current directory. app.UseDirectoryBrowser(); ...
浏览项目的images文件夹,结果如下:
如果有个文件夹在webroot之外,但是需要用户可浏览。项目结构如下:
wwwroot
css
img
js
MyStaticFiles
为了使用户可浏览MyStaticFiles目录,需要如下配置
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... // Add the ability for the user to browse the MyStaticFiles directory. app.UseDirectoryBrowser(new DirectoryBrowserOptions() { FileProvider = new PhysicalFileProvider(@"D:\Source\WebApplication1\src\WebApplication1\MyStaticFiles"), RequestPath = new PathString("/StaticFiles") }); ...
这时,如果如果用户访问地址http:///StaticFiles,就能浏览MyStaticFiles文件夹中的文件。
Serving default files
在用户没有输入完整的URI的情况下,为了提供给用户默认的文件,需要在Startup.Configure中调用UseDefaultFiles。但同事也必须调用UseStaticFiles方法,这是因为UseDefaultFiles是url重写,并不提供文件服务。public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... // Serve the default file, if present. app.UseDefaultFiles(); app.UseStaticFiles(); ...
如果你调用了UseDefaultFiles方法,如果用户输入了某个文件的路径,中间件会按顺序搜索如下文件。如果找到了,文件就会返回,就如同用户输入了完整路径一样。
default.htm
default.html
index.htm
index.html
为了指定其他默认文件,需要实例化DefaultFilesOptions对象,并设置其参数DefaultFileNames列表. 然后调用UseDefaultFiles,并把需要实例化DefaultFilesOptions对象对象传给它。下例,移除了DefaultFileNames列表中的默认的文件,并把mydefault.html添加作为唯一的默认文件。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... // Serve my app-specific default file, if present. DefaultFilesOptions options = new DefaultFilesOptions(); options.DefaultFileNames.Clear(); options.DefaultFileNames.Add("mydefault.html"); app.UseDefaultFiles(options); app.UseStaticFiles(); ...
此时,如果你浏览webroot中的文件夹,并携带文件名mydefault.html,该文件就可以获取,就如同用户输入了全路径。
但是如果你想设置的默认文件在webroot之外,你需要调用UseStaticFiles和UseDefaultFiles,并传给他们相对的参数值。然而更方便和推荐的方法是使用
UseFileServer方法。
使用UseFileServer方法
除了UseStaticFiles, UseDefaultFiles和 UseDirectoryBrowser扩展方法,还有UseFileServer方法,该方法结合了上面三个方法的功能。// Enable all static file middleware (serving of static files and default files) EXCEPT directory browsing. app.UseFileServer();
// Enables all static file middleware (serving of static files, default files, and directory browsing). app.UseFileServer(enableDirectoryBrowsing: true);
如同UseStaticFiles, UseDefaultFiles和UseDirectoryBrowser,如果想服务webroot之外的文件,需要实例化和配置一个“options”对象,并作为参数传给UseFileServer。比如,有如下结构
wwwroot
css
img
js
MyStaticFiles
test.png
default.html
如上结构,如果想提供静态文件,默认文件以及目录浏览功能,如下只需调用UseFileServer方法。
app.UseFileServer(new FileServerOptions() { FileProvider = new PhysicalFileProvider(@"D:\Source\WebApplication1\src\WebApplication1\MyStaticFiles"), RequestPath = new PathString("/StaticFiles"), EnableDirectoryBrowsing = true });
如上例,如果用户访问如下路径
http:///StaticFiles/test.png - MyStaticFiles/test.png 文件会提供给浏览器
http:///StaticFiles - 因为有默认文件default.html,该文件会提供给浏览器。如果不存在默认文件,浏览器会提供文件浏览(因为 FileServerOptions.EnableDirectoryBrowsing属性设置为了true).
文件类型
ASP.NET静态文件中间件提供了接近400种已知的文件类型。如果用户尝试获取位置类型的文件,ASP.NET不会尝试提供服务。假设如下目录
wwwroot
css
js
images
test.image
按如下配置
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... // Serve static files and allow directory browsing. app.UseDirectoryBrowser(); app.UseStaticFiles();
如果访问http:///images这个目录,服务器会列出包括test.image的文件列表。但是如果尝试打开该文件,则会抛出错误。为了服务位置类型的文件,你需要将StaticFileOptions.ServeUnknownFileTypes熟悉nag设置为true并指定默认文件类型。如下
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... // Serve static files and allow directory browsing. app.UseDirectoryBrowser(); app.UseStaticFiles(new StaticFileOptions { ServeUnknownFileTypes = true, DefaultContentType = "image/png" });
这是如果浏览器访问未知类型的文件,服务器会将他们作为image/png类型来对待。
目前为止,在处理为未知类型文件时,已经指定了默认文件类型。但是如果有多种未知文件类型呢。这就是FileExtensionContentTypeProvider类发挥作用的时候了。这个类内部有一个集合,来映射文件扩展名和MIME文件类型。为了自定义文件类型,只需实例化FileExtensionContentTypeProvider,并添加映射到FileExtensionContentTypeProvider.Mappings字典。如下例,添加了扩展.myapp到application/x-msdownload MIME类型。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... // Allow directory browsing. app.UseDirectoryBrowser(); // Set up custom content types - associating file extension to MIME type var provider = new FileExtensionContentTypeProvider(); provider.Mappings.Add(".myapp", "application/x-msdownload"); // Serve static files. app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider }); ...
现在如果用户尝试浏览.myapp后缀名的文件,用户会被提示下载该文件。
IIS Considerations
IIS有自己的静态文件处理模块,它是独立与上文提到的ASP.NET静态文件中间件的。ASP.NET模块是在IIS本地模块之前运行的。在ASP.NET Beta 7中,IIS host改变了运行方式,如果请求没有被ASP.NET所处理,将会返回404错误,而不是交给IIS自身模块来处理。为了运行IIS自身模块,需如下配置。public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... ... // Enable the IIS native module to run after the ASP.NET middleware components. // This call should be placed at the end of your Startup.Configure method so that // it doesn't interfere with other middleware functionality. app.RunIISPipeline(); }
最佳实践
代码文件(包括C#和Razor文件)应该放在项目的webroot之外,这样能够很好的隔离。总结
In this article, you learned how the static files middleware component in ASP.NET 5 allows you to serve static files, enable directory browsing, and serve default files. You also saw how to work with content types that ASP.NET doesn’t recognize. Finally, the article explained some IIS considerations and presented some best practices for working with static files.相关文章推荐
- ASP.NET 写错误日志以及错误引导页
- 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求
- kali 2.0 启动metasploit服务
- Asp.Net WebAPI 中Cookie 获取操作方式
- ASP.NET Web API实现缓存的2种方式
- ASP.NET十分有用的页面间传值方法
- ASP.NET Web API中通过URI显示实体中的部分字段
- JSP与ASP.PHP的比較
- RaspberryPi和Arduino控制温室温度
- PagedDataSource数据绑定控件和AspNetPager分页控件结合使用列表分页
- asp.net中将数据导出
- 在 ASP.NET MVC 3 中应用 KindEditor
- AspNet MVC4 教学-22:Asp.Net MVC4 Partial View 技术高速应用Demo
- 搭建了一个多项目、插件化的ASP.net MVC开发框架
- 搭建了一个多项目、插件化的ASP.net MVC开发框架
- 第一个 Asp.Net vNext 应用程序
- .NET初学者推荐课程 asp.net错误代码大全
- ASP网站项目编译
- asp.net无法触发asp控件的后台方法
- ASP.NET 5基础之中间件