[ASP.NET Core] Static File Middleware

 

前言

本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要的开发人员。

结构

  • 一个Web站台最基本的功能,就是在接收到从「浏览器传入」的HTTP Request封包后,将站台内所提供的静态档案(Static File),封装成为「服务器回传」的HTTP Response封包内容,来提供给浏览器使用。

  • 在ASP.NET Core里,内建了一个Middleware:StaticFileMiddleware,用来建立Web站台提供静态档案的功能。这个Middleware会先剖析HTTP Request封包中的URL路径、然后依照URL路径计算并取得对应的File路径下的档案内容、接着再将该档案内容封装为HTTP Response封包内容,用来提供给浏览器使用。

  • 而在StaticFileMiddleware里,定义URL根路径、File根路径这两个系统参数,来映像URL路径所对应的File路径。用以提供开发人员,灵活的去设定URL路径与File路径之间的关系。

开发

Microsoft.AspNetCore.StaticFiles

在ASP.NET Core里,要加入StaticFileMiddleware来提供静态档案功能。开发人员可以先依照[ASP.NET Core] Getting Started这篇文章里的步骤,来建立相关环境与基本程序代码。接着在project.json里挂载「Microsoft.AspNetCore.StaticFiles」的参考,后续就能使用这个参考里,所提供的StaticFileMiddleware相关对象。

{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
},
"imports": "dnxcore50"
}
}
}

UseStaticFiles()

完成project.json的相关设定之后,就可以回过来修改「Program.cs」。在Microsoft.AspNetCore.StaticFiles里,提供了UseStaticFiles Extension,让开发人员可以方便的挂载StaticFileMiddleware。在下列的范例程序代码里,示范如何透过UseStaticFiles来挂载StaticFileMiddleware。(在StaticFileMiddleware里面,URL根路径默认为:「http://<Url>」、File根路径默认为:「file:\\<ContentRoot>\wwwroot」)。

using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.FileProviders; namespace aspnetcoreapp
{
public class Program
{
public static void Main(string[] args)
{
// Build
var host = new WebHostBuilder() // 设定Host内容的File根路径
.UseContentRoot(Directory.GetCurrentDirectory()) // 设定启动参数
.UseStartup<Startup>() // 开启Kestrel聆听HTTP
.UseKestrel() // 设定聆听的URL
.UseUrls("http://localhost:5000") // 建立Host
.Build(); // Run
try
{
// 启动Host
host.Start(); // 等待关闭
Console.WriteLine("Application started. Press any key to shut down.");
Console.ReadKey();
}
finally
{
// 关闭Host
host.Dispose();
}
}
} public class Startup
{
// Methods
public void Configure(IApplicationBuilder app)
{
// 挂载StaticFilesMiddleware
app.UseStaticFiles();
}
}
}

UseWebRoot(webRoot)

在StaticFileMiddleware里面,File根路径默认为:「file:\\<ContentRoot>\wwwroot」。如果要变更默认的File根路径,开发人员可以使用ASP.NET Core所提供的UseWebRoot Extension来变更默认的File根路径。在下列的范例程序代码里,示范如何透过UseWebRoot来变更默认的File根路径。(范例执行时挂载的StaticFileMiddleware,URL根路径同样为:「http://<Url>」、File根路径变更为:「file:\\<CurrentDirectory>\aaa」)。

using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.FileProviders; namespace aspnetcoreapp
{
public class Program
{
public static void Main(string[] args)
{
// Build
var host = new WebHostBuilder() // 设定Web站台的File根路径
.UseWebRoot(Directory.GetCurrentDirectory() + @"\aaa") // 设定Host内容的File根路径
.UseContentRoot(Directory.GetCurrentDirectory()) // 设定启动参数
.UseStartup<Startup>() // 开启Kestrel聆听HTTP
.UseKestrel() // 设定聆听的URL
.UseUrls("http://localhost:5000") // 建立Host
.Build(); // Run
try
{
// 启动Host
host.Start(); // 等待关闭
Console.WriteLine("Application started. Press any key to shut down.");
Console.ReadKey();
}
finally
{
// 关闭Host
host.Dispose();
}
}
} public class Startup
{
// Methods
public void Configure(IApplicationBuilder app)
{
// 挂载StaticFilesMiddleware
app.UseStaticFiles();
}
}
}

UseStaticFiles(options)

除了使用预设参数挂载StaticFilesMiddleware之外,开发人员也可以使用自定义参数来挂载StaticFilesMiddleware。如果要使用自定义参数来挂载StaticFilesMiddleware,开发人员可以同样使用UseStaticFiles Extension来使用自定义参数挂载StaticFilesMiddleware。在下列的范例程序代码里,示范如何透过UseStaticFiles来挂载StaticFilesMiddleware,并且定义其URL根路径与File根路径。(范例执行时挂载的StaticFileMiddleware,URL根路径变更为:「http://<Url>/bbb」、File根路径变更为:「file:\\<CurrentDirectory>\ccc」)。

using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.FileProviders; namespace aspnetcoreapp
{
public class Program
{
public static void Main(string[] args)
{
// Build
var host = new WebHostBuilder() // 设定Host内容的File根路径
.UseContentRoot(Directory.GetCurrentDirectory()) // 设定启动参数
.UseStartup<Startup>() // 开启Kestrel聆听HTTP
.UseKestrel() // 设定聆听的URL
.UseUrls("http://localhost:5000") // 建立Host
.Build(); // Run
try
{
// 启动Host
host.Start(); // 等待关闭
Console.WriteLine("Application started. Press any key to shut down.");
Console.ReadKey();
}
finally
{
// 关闭Host
host.Dispose();
}
}
} public class Startup
{
// Methods
public void Configure(IApplicationBuilder app)
{
// 挂载StaticFilesMiddleware
app.UseStaticFiles(new StaticFileOptions()
{
// 设定URL根路径
RequestPath = @"/bbb", // 设定File根目录
FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory() + @"\ccc")
});
}
}
}

参考

Static File Middleware的更多相关文章

  1. [ASP.NET Core] Static File Middleware

    前言 本篇文章介绍ASP.NET Core里,用来处理静态档案的Middleware,为自己留个纪录也希望能帮助到有需要的开发人员. ASP.NET Core官网 结构 一个Web站台最基本的功能,就 ...

  2. Asp.net core 学习笔记 ( IIS, static file 性能优化 )

    更新 : 2019-02-06 最后还是把 rewrite 给替换掉了. 所以 rewrite url 也不依赖 iis 了咯. refer : https://docs.microsoft.com/ ...

  3. ASP.NET Core 1.0: 指定Static File中的文件作为default page

    指定一个网站的default page是很容易的事情.譬如IIS Management中,可以通过default page来指定,而默认的index.html, index.htm之类,则早已经被设置 ...

  4. Set a static file on django

    1. In setting file: ROOT_PATH='/home/ronglian/project/taskschedule' STATIC_URL = '/static/' STATICFI ...

  5. Creating a simple static file server with Rewrite--reference

    Today, I’d like to take a quick moment to demonstrate how to make a simple file server using Rewrite ...

  6. 静态文件服务器(The static file servers)

    大部分的网站都会提供一些在通常操作下不会发生改变的资源给浏览器.显示网站外观的图片和CSS文件,在浏览器中运行的JavaScript代码,没有动态组件的HTML文件就是这种资源中的代表,统称为静态文件 ...

  7. 翻译 - ASP.NET Core 基本知识 - 中间件(Middleware)

    翻译自 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-5.0 中间件是集成 ...

  8. ASP.NET5 中静态文件的各种使用方式

    所谓静态文件,包含HTML文件,css文件.图片文件和js文件等,他们是服务器直接读取到客户端的一些资源,在这篇文章中,我们将解释关于ASP.NET5和静态文件的一些内容. 服务端的静态文件 默认情况 ...

  9. ASP.NET Core 1.0基础之静态文件处理

    来源 这些HTML , CSS files, image files, 和JavaScript这些静态文件,是ASP.NET能够直接响应给客户端的.本文详述下ASP.NET和静态文件的关系. Serv ...

随机推荐

  1. 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

    关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...

  2. Linux操作系统的简单认识

    1:Linux操作系统的历史 Linux操作系统是由unix操作系统发展而来的,但是Unix是收费的系统,而Linux操作系统的免费的,开源的,所以使用比较广泛,但是它是基于命令行的,不提供图形化用户 ...

  3. 关于Spring的IOC和DI

    原始调用模型 Spring的演化过程 Spring的调用过程 ======================================= IoC[理解][应用][重点] 1.IoC(Inversi ...

  4. HuffmanTree && HuffmanCode

    如何构造HuffmanTree? Huffman算法: (1)根据给定的n个权值{w1, w2, ...,wn}构成n棵二叉树集合

  5. strstr,strchr,strtr 比较

    strstr - 查找字符串的首次出现 查找 "Shanghai" 在字符串中的第一次出现,并返回字符串的剩余部分: echo strstr("I love Shangh ...

  6. Ubuntu 修改时间

    输入"date",显示的是: Tue Jan :: UTC 输入"date -R" 显示的是: Tue, Jan :: + 和北京时间相差了8个小时. 1.选择 ...

  7. JavaScript高级程序设计(第三版)学习笔记8、9、10章

    第8章,BOM BOM的核心对象是window,具有双重角色,既是js访问浏览器的一个接口,又是ECMAScript规定的Global对象.因此,在全局作用域中声明的函数.变量都会变成window对象 ...

  8. PHP之curl

    当我第一次接触curl的时候,看文档,以及网上search各种资料,官方(http://cn2.php.net/manual/en/intro.curl.php)的解释是,这是某大牛写的一个libcu ...

  9. 用ASP生成RSS

    <% Response.Clear Response.CharSet="gb2312" '数据集 Response.ContentType="text/xml&qu ...

  10. 常用meta整理【转载】

    < meta > 元素 概要 标签提供关于HTML文档的元数据.元数据不会显示在页面上,但是对于机器是可读的.它可用于浏览器(如何显示内容或重新加载页面),搜索引擎(关键词),或其他 we ...