- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望我的静态内容(图像、javascript 文件、css 文件等)仅在文件更新后才能完整提供。
如果文件自上次请求以来没有更改(由 ETag
和 Last-Modified
响应 header 值确定),那么我希望客户端浏览器使用文件的缓存版本。
Nancy 是否支持此功能?
最佳答案
Nancy 确实部分支持 ETag
和 Last-Modified
标题。它为所有静态文件设置它们,但从版本 开始0.13 它对这些值没有任何作用。这是南希代码:
Nancy.Responses.GenericFileResponse.cs
if (IsSafeFilePath(rootPath, fullPath))
{
Filename = Path.GetFileName(fullPath);
var fi = new FileInfo(fullPath);
// TODO - set a standard caching time and/or public?
Headers["ETag"] = fi.LastWriteTimeUtc.Ticks.ToString("x");
Headers["Last-Modified"] = fi.LastWriteTimeUtc.ToString("R");
Contents = GetFileContent(fullPath);
ContentType = contentType;
StatusCode = HttpStatusCode.OK;
return;
}
ETag
和
Last-Modified
您需要添加一些修改后的扩展方法的 header 值。我直接从 GitHub 的 Nancy 源代码中借用了这些(因为这个功能计划在 future 发布),但最初的想法来自 Simon Cropp -
Conditional responses with NancyFX
public static void CheckForIfNonMatch(this NancyContext context)
{
var request = context.Request;
var response = context.Response;
string responseETag;
if (!response.Headers.TryGetValue("ETag", out responseETag)) return;
if (request.Headers.IfNoneMatch.Contains(responseETag))
{
context.Response = HttpStatusCode.NotModified;
}
}
public static void CheckForIfModifiedSince(this NancyContext context)
{
var request = context.Request;
var response = context.Response;
string responseLastModified;
if (!response.Headers.TryGetValue("Last-Modified", out responseLastModified)) return;
DateTime lastModified;
if (!request.Headers.IfModifiedSince.HasValue || !DateTime.TryParseExact(responseLastModified, "R", CultureInfo.InvariantCulture, DateTimeStyles.None, out lastModified)) return;
if (lastModified <= request.Headers.IfModifiedSince.Value)
{
context.Response = HttpStatusCode.NotModified;
}
}
AfterRequest
调用这些方法。卡在你的 Nancy BootStrapper 中。
public class MyBootstrapper :DefaultNancyBootstrapper
{
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
pipelines.AfterRequest += ctx =>
{
ctx.CheckForIfNoneMatch();
ctx.CheckForIfModifiedSince();
};
base.ApplicationStartup(container, pipelines);
}
//more stuff
}
200 - OK
状态码。
304 - Not Modified
状态码。文件更新后,再次请求它会使用
200 - OK
下载它。状态码...等等。
关于browser-cache - NancyFX 是否支持通过 ETag 和 Last-Modified header 进行静态内容缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12726113/
我有一个系统,我需要在其中显示记录列表,这样我们可以使用三个选项 上周 上个月 去年 我的表结构有一个名为 createdate 的字段,它是 BIGINT 类型并保存从 PHP 的 time() 函
假设,我对某些功能使用哪一个并不重要,是否使用它会在性能方面产生任何差异 $('div:last'); 或 $('div').last(); 谢谢! 最佳答案 last() 在大型 DOM 集上明显比
有人可以解释 .last() 和 :last 之间的区别吗?我似乎找不到明确的解释。 为什么 $('td.cellsOfSpecificClass:last', '.table tr') 返回每个 t
我想用 li:last 做点什么: var p = $("li:last"); 我需要它的位置:position.left 通过该位置,我可以对齐一些元素。问题是,在某些情况下,最后一个 li 被 e
请看这张图片: 谁能解释一下其中的区别吗? 编辑 让我指出什么让我困惑。请注意: $row.is('tr.items:last') === false $row[0].id === $('tr.ite
我注意到 $( 'filter:last' ) 与 jQuery 中的 $( 'filter:last-child' ) 不同。 我尝试了 jQuery 文档,但很难理解 :last 的额外用途以及它
我正在尝试使用 CSS 选择最后一个 col-xs-12 div 中包含的最后一个元素。关键是元素是动态的,所以它可以是 h2 或 h3 等。
当我使用 :last-child 定位 div 时,它不起作用。使用 :first-child 没关系。 :last-of-type 也可以。有任何想法吗?谢谢。 HTML Lorem
HTML: ... ... ... ... CSS: .plan-box:last-of-type { ... } 在上面的 CSS 代码中,如果我在
我想知道 .filter(':last') 和 .last() 之间是否有任何区别? 对我来说,他们似乎也在做同样的事情,但我是 jQuery 的新手。如果结果没有差异,推荐使用哪一种还是只是个人喜好
std::adjacent_find searches the range [first, last) for two consecutive identical elements. Return v
这个问题在这里已经有了答案: How can I select the last element with a specific class, not last child inside of pa
目标 创建一个辅助列,将单元格的值从 Last, First 转换至First Last 下面的公式工作正常。 A1包含 Last, First下面的公式转换为所需的输出。 A2 = MID(A1,
我需要获取歌曲的发行日期。 在 last.fm API 中,如文档中所述,足以向服务器发出 HTTP 请求,它将使用包含字段“”的 XML(或 JSON)进行回复(如示例响应中所示在网站上)。 问题是
类似于 .Last.value有什么办法可以访问上次通话吗?低于预期的潜在结果.Last.call . sum(1, 2) # [1] 3 str(.Last.call) # language su
在 Perl 中调用 last 而不是在 C 中调用 break 的历史原因是什么? Perl 的设计受到 C 的影响(此外还有 awk、sed 和 sh - 请参阅下面的手册页),因此不采用熟悉的
我正在尝试交换字符串中的两个单词。我目前有一个 txt 文件,其中有一列用户格式为 last.first。我如何将它交换为 first.last? 最佳答案 -split 字符串并连接: $Last,
我有以下 html 代码: Text1 Text2 Text3 使用: nav :last-child { text-transform: upperca
我主要是一名Python程序员,正在学习一些Java。我需要一个函数将包含“First Last”形式的名称的字符串转换为“Last, First”(我还需要它能够处理单个名称:“Cher”=>“Ch
难以尝试将集合中的特定元素作为最后一个匹配的选择器。 this is today this is today
我是一名优秀的程序员,十分优秀!