我已經覆蓋了函數GetVirtualFileSources()如下面的鏈接表示,但我不壓縮文件:https://github.com/ServiceStack/ServiceStack/wiki/HTML,-CSS-and-JavaScript-Minification#minify-dynamic-razor-viewsServiceStack縮小
請有助於理解這個問題。
預先感謝您!
編輯
GetVirtualFileSources()精縮文件,並添加內存文件系統,但是當我提出要求http://myshost/style.css我得到的原始文件。
如何使後端返回最小化的文件。
它requet原料:
GET /content/style.css HTTP/1.1
Host: localhost:61923
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Accept: text/css,*/*;q=0.1
Referer: http://localhost:61923/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: ss-pid=1BFULq391BqiGZ4dwj3Z; X-UAId=2; com.cloudloto.lang=ru; ss-id=GaurYdBT7yDywtF3JTiM
EDIT 2
性反應的原料:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/css
Last-Modified: Sat, 27 May 2017 23:22:08 GMT
Accept-Ranges: bytes
Vary: Accept
Server: Microsoft-IIS/10.0
X-Powered-By: ServiceStack/4,58 Win32NT/.NET
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Credentials: true
X-Startup-Errors: 1
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?ZDpcRG9jdW1lbnRzXExvdG9UZWFtXGxvdG9cbG90b1xsb3RvXGNvbnRlbnRcc3R5bGUuY3Nz?=
X-Powered-By: ASP.NET
Date: Sun, 28 May 2017 20:55:01 GMT
Content-Length: 50176
我重視的文件,而不是直接由虛擬路徑:
<link href="/content/style.css" rel="stylesheet" />
文件只是最小化,其餘我作爲文檔。
public override List<IVirtualPathProvider> GetVirtualFileSources() {
var existingProviders = base.GetVirtualFileSources();
var memFs = new InMemoryVirtualPathProvider(this);
var fs = existingProviders.First(x => x is FileSystemVirtualPathProvider);
foreach (var file in fs.GetAllMatchingFiles("*.html")) {
var contents = Minifiers.HtmlAdvanced.Compress(file.ReadAllText());
memFs.AddFile(file.VirtualPath, contents);
}
foreach (var file in fs.GetAllMatchingFiles("*.css")
.Where(file => !file.VirtualPath.EndsWith(".min.css"))) //ignore pre-minified .css
{
var contents = Minifiers.Css.Compress(file.ReadAllText());
memFs.AddFile(file.VirtualPath, contents);
}
foreach (var file in fs.GetAllMatchingFiles("*.js")
.Where(file => !file.VirtualPath.EndsWith(".min.js"))) //ignore pre-minified .js
{
try {
var js = file.ReadAllText();
var contents = Minifiers.JavaScript.Compress(js);
memFs.AddFile(file.VirtualPath, contents);
} catch (Exception ex) {
base.OnStartupException(new Exception("JSMin Error {0}: {1}".Fmt(file.VirtualPath, ex.Message)));
}
}
existingProviders.Insert(0, memFs);
return existingProviders;
}
感謝。
您尚未提供任何可幫助確定問題的信息。請始終顯示您在問題中使用的impl。首先調試您的自定義GetVirtualFileSources()impl以確認此.css文件正被縮小並添加到Memory Filesystem中,請注意VirtualPath然後直接從Chrome請求該css文件以驗證它是否縮小,請顯示HTTP請求只有在WebInspector中,源視圖可能會自動格式化。 – mythz
還請包括原始HTTP響應頭。你的網址也是'http:// myshost/style.css',但你的HTTP請求表明它的請求'/ content/style.css'。請指定未被縮小的確切虛擬路徑,並確認是否縮小了確切的文件並將其添加到'GetVirtualFileSources()'中的memFs中。 – mythz
我假設您向我展示的調試輸出是針對具有VirtualPath「/content/style.css」的文件?響應也由ServiceStack提供,所以它應該工作,因爲ServiceStack使用VirtualFileSource來源靜態文件,除非你有一些其他的配置干擾它,但我不能從這裏告訴,因爲它的工作正常。你可以在我的答案中嘗試服務實現,並讓我知道返回的文件是縮小版本嗎? – mythz