- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的 Web API 2 应用程序中处理 HEAD 请求。我从 StrathWeb 的博客文章中复制粘贴了代码 Adding HTTP HEAD support to ASP.NET Web API :
public class HeadHandler : DelegatingHandler
{
private const string Head = "IsHead";
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (request.Method == HttpMethod.Head)
{
request.Method = HttpMethod.Get;
request.Properties.Add(Head, true);
}
var response = await base.SendAsync(request, cancellationToken);
object isHead;
response.RequestMessage.Properties.TryGetValue(Head, out isHead);
if (isHead != null && ((bool) isHead))
{
var oldContent = await response.Content.ReadAsByteArrayAsync();
var content = new StringContent(string.Empty);
content.Headers.Clear();
foreach (var header in response.Content.Headers)
{
content.Headers.Add(header.Key, header.Value);
}
content.Headers.ContentLength = oldContent.Length;
response.Content = content;
}
return response;
}
}
当我从 Fiddler 发出 HEAD 请求时:
HEAD http://localhost:54225/api/books/5 HTTP/1.1
User-Agent: Fiddler
Host: localhost:54225
我收到以下回复:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 0
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcc2ltb25lXERvY3VtZW50c1xTaW1vbnNEb2N1bWVudHNcSVRcQyNcRGVtb0NvZGVcUkVTVGZ1bFdlYlNlcnZpY2VDbGllbnRERU1PXEJvb2tTZXJ2aWNlXGFwaVxib29rc1w1?=
X-Powered-By: ASP.NET
Date: Sun, 19 Feb 2017 12:09:52 GMT
Content-Length 为 0 但应为 104 字节。
当我向 HeadHandler 添加断点并逐步执行代码时,它似乎在返回响应之前将 Content-Length header 值正确设置为 104 字节。
在 HeadHandler 之后运行的管道中是否有其他步骤,它识别响应没有主体,并将 Content-Length 设置为 0?添加 HeadHandler 之后 WebApiConfig.cs 中唯一的事情是 HttpAttribute 路由的映射和默认路由的设置,在我看来这两者都不太可能导致 Content-Length header 被重置。或者,某处的配置设置是否可能影响响应中返回的 Content-Length?
最佳答案
您需要做的就是设置响应的 Content.Headers 的 Content-Length - 无需为响应创建新内容。无论如何,请求 HEAD 的行为都会从响应中删除正文内容。
这可能是自您引用的文章撰写以来(2013 年)发生的变化,不再需要从头开始创建内容...
这就是你所需要的:
public class HeadHandler : DelegatingHandler
{
private const string Head = "IsHead";
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (request.Method == HttpMethod.Head)
{
request.Method = HttpMethod.Get;
request.Properties.Add(Head, true);
}
var response = await base.SendAsync(request, cancellationToken);
object isHead;
response.RequestMessage.Properties.TryGetValue(Head, out isHead);
if (isHead != null && ((bool)isHead))
{
var oldContent = await response.Content.ReadAsByteArrayAsync();
response.Content.Headers.ContentLength = oldContent.Length;
return response;
}
return response;
}
}
关于c# - 在 Web API 2 中处理 HEAD 请求时的 Content-Length 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42327329/
在git中,以下有什么区别? 头 头^ HEAD~1 HEAD~2 它们与 master 有什么关系?那么有没有MASTER^, MASTER~1?? 最佳答案 HEAD 是当前分支上最新提交的同义词
我想实现一个 LinkedListremove 方法,该方法可以删除任何特定位置的项目,但在本例中,我最感兴趣的是删除列表开头(第 0 个位置)的项目。 我的代码适用于 n 大于零的值,因此我为 n=
的顺序有什么关系吗?或 或 标签位于 中? (愚蠢的问题,但其中一件事我直到现在才考虑过。) 最佳答案 优化 据 Yahoo! 的人说你应该put CSS at the top和 scripts
HEAD 是指向当前分支的指针。我见过 HEAD 祖先的各种符号,包括 HEAD~2 HEAD^2 HEAD@{2} 头~~ 头^^ 以上每一项到底是什么意思?这方面的文件在哪里? 最佳答案 来自文档
我对 git 很陌生,任何人都可以帮助我。 我实际上被困在什么是“git diff HEAD”。 “git diff HEAD”和“git diff HEAD HEAD~1”有什么区别 最佳答案 gi
我似乎不知道如何获得 git_reference *到特定 Remote 的 HEAD。 我有: git_repository * repo = NULL; git_reference * ref
关于 firefox浏览器控制台,我可以访问 容器如下所示。 > window [object Window] > documentObject = window["document"
我使用 asp.net 4 和 c#。 我在 Web From 页面中有一个 Web 用户控件。当我包含 Web 用户控件时,我还想以编程方式在最终生成的页面的标记中包含一些脚本。 知道怎么做吗?也许
这可能是一个非常愚蠢的问题,但是加载文件中标签之间的数据并通过 PHP include() 加载它是愚蠢的吗?这样就可以更轻松地进行编辑。谢谢。 最佳答案 不,这不会是愚蠢的。代码重复越少越好。 关于
我编写了以下 facelet index.xhtml: 当我从浏览器获取
我有一个不断递增的“指针”,我需要最终返回指针的“头”。我进退两难,要么使用“pointer[0]”,要么使用另一个名为“head”的变量并初始化它并在最后返回。我觉得前者使代码看起来很脏,后来占用的
git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME_PRODUCTION.git HEAD 和 git pu
当我像这样将 Git 子模块添加到 Git 存储库时, git submodule add ssh://server/proj1/ proj1 git submodule init git submo
我的本地存储库中有一个我不打算推送的提交(A)。现在,HEAD 在 A 处。假设我想在提交 (A) 之上创建另一个提交 (B) NOT,但在前一个提交之上而不是提交 (A)。 如何在不丢失最新
多次调用 head.load 是否同步? 我的意思是,如果我们有这样的代码: head.load('scr1.js',...,'scr8.js'); head.load('scr11.js',...,
执行以下查询时出现以下错误: #1054 - Unknown column 'headings.heading' in 'field list' 标题表中肯定有一个名为“标题”的列。当我测试它时,问题
我试图显示上次提交与之前提交之间的差异: git diff HEAD^ HEAD 但是什么都不显示。事实上,我知道这两个提交之间存在差异。 我做错了什么,我应该如何改正? P.S.:我觉得这个问题以
这就是场景。我有一个运行一些测试的脚本。我需要制作另一个接受 git 提交名称作为参数的脚本,然后执行以下操作: 保存当前提交状态 - 分支名称或未命名提交。 在指定提交时切换到分离的 HEAD 针对
我发现自己经常输入这个,比如当我做了一些改变,提交它,然后要么需要查找我在那里做的事情来弄清楚下一步该做什么,要么确保我没有添加任何意外的东西在将其推送到远程之前提交。 无可否认,diff HEAD^
我是否理解正确,Git head(小写)和 Git HEAD(大写)的区别在于前者是结束提交,后者只是当前提交(无论是最终提交还是非最终提交被选为 HEAD 提交)? 编辑:“结束提交”是指“给定分支
我是一名优秀的程序员,十分优秀!