[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. Azure PowerShell 创建虚拟机

    # 指定订阅名称$subscriptionName="订阅名称"# 指定云服务名称$serviceName="云服务名称"# 指定用来保存虚拟机VHD的存储$s ...

  2. JUC全景图

    JUC 并发编程全景图如下:

  3. 在 Ubuntu 16.04 中安装谷歌 Chrome 浏览器

    进入 Ubuntu 16.04 桌面,按下 Ctrl + Alt + t 键盘组合键,启动终端. 也可以按下 Win 键(或叫 Super 键),在 Dash 的搜索框中输入 terminal 或&q ...

  4. 【Objective-C】0-第一个OC的类

    OC是一门面向对象的语言,因此它也有类.对象.静态\动态方法.成员变量的概念.这讲就来创建第一个OC的类. 一.语法简介 1.类 在Java中,我们用1个.java文件就可以描述清楚一个类:在OC中, ...

  5. 轻松学习Ionic (一) 搭建开发环境,并创建工程

    1.准备工作     下载 Node.js(下载包),WebStorm(IDE,编写代码,浏览器调试),JDK(webstorm 运行环境),Android SDK (Android编译)     不 ...

  6. ajax_jsonp —— 跨域

    JSONP:原理是script标签 一.抓包 二.不用每次都连接 localhost 的方法   三.抓包后所需的参数 su?:后面跟的是传递过去的参数. cb:是 callback 后面跟的是对返回 ...

  7. h2database源码浅析:事务、两阶段提交

    Transaction Isolation Transaction isolation is provided for all data manipulation language (DML) sta ...

  8. C++多态性与C#的比较

        多态性:统一操作作用于不同的对象可以有不同的解释,产生不同的执行结果.多态性可以分为两种:一是编译时的多态性,一是运行时的多态性.     编译时的多态性包括重载.覆盖.运算符重载.对于非虚的 ...

  9. linux时间自动同步

    1,修正本地时区及ntp服务 #yum -y install ntp#rm -rf /etc/localtime#ln -s /usr/share/zoneinfo/Asia/Shanghai /et ...

  10. oc语言学习之基础知识点介绍(五):OC进阶

    一.点语法介绍 /* 以前封装后,要给属性赋值,必须调用方法 这样做,有两个缺点: 1.代码量多,调用方法要写的东西多. 2.看起来并不像是给属性赋值,也不像取值. 我们用点语法就可以更好的解决! 点 ...