更新 : 2020-05-22

https://stackoverflow.com/questions/36866057/c-sharp-tool-that-formats-an-html-string-before-sending-it-to-the-client

手动调用 mini html 也是 ok 的

然后发现, whitespace 无法 clear 掉 span new line 这种情况.

https://css-tricks.com/fighting-the-space-between-inline-block-elements/

span new line 会有 space, 但是这个是 html 的设计啦,不 clear 因为这样比较安全, 不会因为 mini 而破坏最终的呈现.

更新 : 2019-05-04

补上一个完整的 startup.cs

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services)
{
var fileExtensionContentTypeProvider = new FileExtensionContentTypeProvider();
fileExtensionContentTypeProvider.Mappings[".webmanifest"] = "application/manifest+json";
services.AddSingleton<IContentTypeProvider>(fileExtensionContentTypeProvider);
services.Configure<RewriteOptions>(options =>
{
options.AddRedirectToWww();
});
services.Configure<RewriteOptions>(options =>
{
options.AddRedirectToHttps();
});
services.AddResponseCompression(options => {
options.EnableForHttps = true;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
} public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
{
app.UseRewriter();
app.UseResponseCompression(); if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
} app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = serviceProvider.GetService<IContentTypeProvider>(),
OnPrepareResponse = ctx =>
{
if (!env.IsDevelopment())
{
var cachePeriod = TimeSpan.FromDays( * ).TotalSeconds.ToString();
ctx.Context.Response.Headers.Append("Cache-Control", $"public, max-age={cachePeriod}");
}
}
});
app.UseMvc();
}
}

还有 web config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\Project.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
<urlCompression doStaticCompression="false" />
<modules>
<remove name="RewriteModule" />
<remove name="AspNetCoreModule" />
<remove name="RequestFilteringModule" />
<remove name="iisnode" />
<remove name="ProtocolSupportModule" />
<remove name="IsapiModule" />
<remove name="IpRestrictionModule" />
<remove name="IsapiFilterModule" />
<remove name="HttpRedirectionModule" />
<remove name="DynamicIpRestrictionModule" />
<remove name="DirectoryListingModule" />
<remove name="DefaultDocumentModule" />
<remove name="CorsModule" />
<remove name="ConfigurationValidationModule" />
<remove name="ApplicationInitializationModule" />
<remove name="WebSocketModule" />
<remove name="AnonymousIdentification" />
<remove name="DefaultAuthentication" />
<remove name="FileAuthorization" />
<remove name="FormsAuthentication" />
<remove name="Profile" />
<remove name="OutputCache" />
<remove name="UrlAuthorization" />
<remove name="RoleManager" />
<remove name="ScriptModule-4.0" />
<remove name="UrlMappingsModule" />
<remove name="UrlRoutingModule-4.0" />
<remove name="WindowsAuthentication" />
<remove name="Session" />
<remove name="StaticFileModule" />
<remove name="StaticCompressionModule" />
</modules>
<httpErrors errorMode="Detailed" />
</system.webServer>
</location>
</configuration>
<!--ProjectGuid: 1a4d84c4-a1de-4b9f-b1ee-b0912a57ceb6-->

更新 : 2019-02-06

最后还是把 rewrite 给替换掉了. 所以 rewrite url 也不依赖 iis 了咯.

refer : https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-2.2&tabs=visual-studio

上面这篇说明了如何使用 http redirect to https

现在 https 都免费了嘛, 网站当然是肯定要 https 丫.

https://certifytheweb.com/ 我用这个来制作证书... 非常方便哦.

除了 https 还有 non-www redirect to www

refer

https://stackoverflow.com/questions/43823413/redirect-non-www-to-www-using-asp-net-core-middleware

https://github.com/aspnet/BasicMiddleware/pull/297

也是原生支持的哦.

note :  一些坑和额外知识

有一个东西叫 HSTS, asp.net core 也有支持.

refer :

https://blog.wilddog.com/?page_id=1493

https://www.barretlee.com/blog/2015/10/22/hsts-intro/

如果你做 https 那就一并做了吧.

它是配合游览器的一个东西,大部分游览器都有支持了.

主要就是防止窃听, 当用户第一次访问服务器后,服务器会告诉 browser, 我这个网站只能用 https 访问.

那么,往后即使用户在地址栏输入没有 http 的网址,游览器也会自动换成 https.

还有一点就是,如果证书过期了,用户无法忽略它,游览器会完全禁止不安全的访问.

上面的前提是至少访问一次, 那如果你要更极端一点,可以提交你的网站给游览器厂商, 那么连第一次访问也都必须是 https.

更新 : 2019-02-05

refer :

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2

https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-2.2

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/modules?view=aspnetcore-2.2

依赖 IIS 来处理 static file, 缓存, 压缩, 未必就好,

要知道 IIS 是一个大家伙, 很多历史包袱, 而且绑定 windows 服务器. 这并不符合个 asp.net core 跨平台的特色.

所以呢, 我们可以尽量减少对 IIS 的依赖.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2

这篇说了如果使用 asp.net core 来处理静态文件和 cache control.

https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-2.2

这篇则说了如何做压缩文件.

这样我们就不需要像从前我说的那样处理了.

那么还有一个重点是, IIS 和 asp.net core 会不会打架呢? 你要处理,我也要处理...

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/modules?view=aspnetcore-2.2

这篇说了如何关掉 IIS 的处理 Module

我是使用 webconfig 来关闭的,因为还有一些古老项目在服务器上跑着.

记得去 C:\Windows\System32\inetsrv\config 这里打开权限, 不然 webconfig 无法 remove module 哦.

最后 iis 剩下的 module

anony 和 aspnetcoremodule 是一定要的, uriCache 和 httpcache 是微软推荐要的 (asp.net core 推荐)

rewirte 用于 https and www, asp.net core 有自己的 middle 不要也可以拿掉

custom error 和 logging 只是为了方便 debug.

note :  一些坑和额外知识

1.https 情况下要开,默认是不开的, 开之前了解一下 https 安全 CRIME and BREACH attacks.

services.AddResponseCompression(options => {
options.EnableForHttps = true;
});

2. Use.. 一定要 before staticfile() and mvc()

app.UseResponseCompression();

3. 压缩 br 格式是 google 发明的, 视乎比 gzip 还好哦, 但是不用担心, asp.net core br 和 gzip 都是默认支持

更新 : 2019-02-05

refer :

https://weblog.west-wind.com/posts/2017/Apr/27/IIS-and-ASPNET-Core-Rewrite-Rules-for-Static-Files-and-Html-5-Routing

https://weblogs.asp.net/imranbaloch/leveraging-iis-static-file-feature-in-aspnetcore

https://shiyousan.com/post/635659901987610088

https://blogs.iis.net/nazim/use-of-special-characters-like-in-an-iis-url

一直以来 url 我基本上是不放富豪的, 但最近有个项目比较奇葩, 用了空格作为 filename

然后就发现之前的表达式不支持 %20 空格的处理, 就改了改

^.*\.(html|htm|svg|json|css|png|gif|jpg|jpeg|js|mp3|mp4|woff2|woff|ico|pdf|webmanifest)$

于是就随便试试其它符号, 发现 + 和 % 也是不可以。

经过测试不是 rewrite 的问题, 读了 iis 文档后 https://blogs.iis.net/nazim/use-of-special-characters-like-in-an-iis-url

看样子是被禁止了, 最后总结, 还是不要乱用符号在文件上吧.

refer :

https://weblog.west-wind.com/posts/2017/Apr/27/IIS-and-ASPNET-Core-Rewrite-Rules-for-AspNetCoreModule

https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?tabs=aspnetcore2x

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files

https://andrewlock.net/html-minification-using-webmarkupmin-in-asp-net-core/

https://github.com/Taritsyn/WebMarkupMin

core host in IIS 会拦截所有的请求, 包括静态文件

可是 IIS 处理静态文件压缩等等还是不错的

那我们可以在 webconfig 里去设置一下

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="wwwroot-static">
<match url="([\S]+[.](html|htm|svg|js|css|png|gif|jpg|jpeg|json|mp3|mp4|woff|woff2))" />
<action type="Rewrite" url="wwwroot/{R:1}" />
</rule>
</rules>
</rewrite>
<handlers>
<add name="StaticFileModuleSvg" path="*.svg" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFileModuleJs" path="*.js" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFileModuleCss" path="*.css" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFileModuleJpeg" path="*.jpeg" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFileModuleJpg" path="*.jpg" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFileModulePng" path="*.png" verb ="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFileModuleGif" path="*.gif" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFileModuleJson" path="*.json" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFileModuleMp4" path="*.mp4" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFileModuleMp3" path="*.mp3" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFileModuleWoff" path="*.woff" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFileModuleWoff2" path="*.woff2" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\Project.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
<staticContent>
<remove fileExtension=".woff" />
<mimeMap fileExtension=".woff" mimeType="application/font-woff" />
<remove fileExtension=".json" />
<mimeMap fileExtension=".json" mimeType="application/json" />
<remove fileExtension=".mp4" />
<mimeMap fileExtension=".mp4" mimeType="audio/mp4" />
<remove fileExtension=".ogg" />
<mimeMap fileExtension=".ogg" mimeType="audio/ogg" />
<!--local iis no allow below-->
<remove fileExtension=".less"/>
<mimeMap fileExtension=".less" mimeType="text/css" />
<remove fileExtension=".woff2"/>
<mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
<clientCache cacheControlMode="UseExpires" httpExpires="Tue, 12 Jan 2027 03:14:07 GMT" />
</staticContent>
<defaultDocument>
<files>
<clear />
<add value="index.aspx" />
<add value="Default.aspx" />
<add value="index.html" />
<add value="Default.html" />
</files>
</defaultDocument>
</system.webServer>
</configuration>

做一个 rewrite 还有 handlers 就可以了.

如果要处理 html minify 可以使用这个 plugin WebMarkupMin

Asp.net core 学习笔记 ( IIS, static file 性能优化 )的更多相关文章

  1. Asp.Net Core学习笔记:入门篇

    Asp.Net Core 学习 基于.Net Core 2.2版本的学习笔记. 常识 像Django那样自动检查代码更新,自动重载服务器(太方便了) dotnet watch run 托管设置 设置项 ...

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

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

  3. ASP.NET Core 学习笔记 第一篇 ASP.NET Core初探

    前言 因为工作原因博客断断续续更新,其实在很早以前就有想法做一套关于ASP.NET CORE整体学习度路线,整体来说国内的环境的.NET生态环境还是相对比较严峻的,但是干一行爱一行,还是希望更多人加入 ...

  4. Asp.net Core学习笔记

    之前记在github上的,现在搬运过来 变化还是很大的,感觉和Nodejs有点类似,比如中间件的使用 ,努力学习ing... 优点 不依赖IIS 开源和跨平台 中间件支持 性能优化 无所不在的依赖注入 ...

  5. ASP.NET Core 学习笔记 第三篇 依赖注入框架的使用

    前言 首先感谢小可爱门的支持,写了这个系列的第二篇后,得到了好多人的鼓励,也更加坚定我把这个系列写完的决心,也能更好的督促自己的学习,分享自己的学习成果.还记得上篇文章中最后提及到,假如服务越来越多怎 ...

  6. ASP.NET Core 学习笔记 第四篇 ASP.NET Core 中的配置

    前言 说道配置文件,基本大多数软件为了扩展性.灵活性都会涉及到配置文件,比如之前常见的app.config和web.config.然后再说.NET Core,很多都发生了变化.总体的来说技术在进步,新 ...

  7. ASP.NET Core 学习笔记 第五篇 ASP.NET Core 中的选项

    前言 还记得上一篇文章中所说的配置吗?本篇文章算是上一篇的延续吧.在 .NET Core 中读取配置文件大多数会为配置选项绑定一个POCO(Plain Old CLR Object)对象,并通过依赖注 ...

  8. Asp.net core 学习笔记 ( Web Api )

    asp.net core 把之前的 webapi 和 mvc 做了结合. mvc 既是 api. 但是后呢,又发现, api 确实有独到之处,所以又开了一些补助的方法. namespace Proje ...

  9. ASP.NET Core 学习笔记 第二篇 依赖注入

    前言 ASP.NET Core 应用在启动过程中会依赖各种组件提供服务,而这些组件会以接口的形式标准化,这些组件这就是我们所说的服务,ASP.NET Core框架建立在一个底层的依赖注入框架之上,它使 ...

随机推荐

  1. Js扩容

    /*脚本统一调用工具(企业端)*/ //杂项工具 var MiscUtils = { //去除字符串中所有的空格 ClearStringEmpty: function (str) { var strR ...

  2. Docker学习记录--入门了解+安装

    Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制, ...

  3. Shell脚本,更改Info.plist中的日期等

    #!/bin/bashroot_src=$(dirname $(PWD)) bundle_name='RandomDebbot.bundle' target_path=$root_src/ecovac ...

  4. topcoder srm 663 div1

    problem1 link 每次枚举$S$的两种变化,并判断新的串是否是$T$的子串.不是的话停止搜索. problem2 link 首先考慮增加1个面值为1的硬币后,$ways$数组有什么变化.设原 ...

  5. Python3 tkinter基础 Canvas create_text 在画布上添加文字

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. Restful framework【第二篇】APIView

    安装djangorestframework 方式一:pip3 install djangorestframework 方式二:pycharm图形化界面安装 方式三:pycharm命令行下安装(装在当前 ...

  7. HDU 6318 Swaps and Inversions(归并排序 || 树状数组)题解

    题意:一个逆序对罚钱x元,现在给你交换的机会,每交换任意相邻两个数花钱y,问你最少付多少钱 思路:最近在补之前还没过的题,发现了这道多校的题.显然,交换相邻两个数逆序对必然会变化+1或者-1,那我们肯 ...

  8. c# 之partial(分部代码和分部类)

    using System; namespace Partial { class Program { static void Main(string[] args) { A a = new A(); } ...

  9. Mysql视图、触发器、事务、储存过程、函数

    一.视图 什么是视图 视图是有一张表或多张表的查询结果构成的一张虚拟表 为什么使用视图 当我们在使用多表查询时 我们的sql语句可能会非常的复杂,如果每次都编写一遍sql'的话无疑是一件麻烦的事情,这 ...

  10. 51nod 1689 逛街(优先队列)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1689 题意: 题意: 枚举终点,这样就确定路上的花费,接下来只需要计算进 ...