使用 ASP.NET MVC 响应 HTTP HEAD 请求

发布于 2024-09-08 03:05:57 字数 413 浏览 6 评论 0原文

当机器人使用 HEAD 访问我的 ASP.NET MVC 站点时,我希望正确支持 HTTP HEAD 请求。我注意到,对该网站的所有 HTTP HEAD 请求都返回 404,特别是来自 http://downforeveryoneorjustme.com 。这真的很烦人。希望他们能像其他所有优秀的机器人一样切换到 GET。

如果我只是将 [AcceptVerbs(HttpVerbs.Get)] 更改为 [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)] MVC 会知道删除请求正文吗?

您做了什么来支持 HTTP HEAD 请求? (代码示例会很棒!)

I'd like to correctly support the HTTP HEAD request when bots hit my ASP.NET MVC site using HEAD. It was brought to my attention that all HTTP HEAD requests to the site were returning 404s, particularly from http://downforeveryoneorjustme.com. Which is really annoying. Wish they would switch to GET like all the other good bots out there.

If I just change [AcceptVerbs(HttpVerbs.Get)] to [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)] will MVC know to drop the body of the request?

What have you done to support HTTP HEAD requests? (Code sample would be great!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜嗑 2024-09-15 03:05:57

我在 ASP.Net MVC 2 项目中创建了一个简单的操作方法:

public class HomeController : Controller
{
    public ActionResult TestMe()
    {
        return View();
    }
}

然后启动 Fiddler 并构建了一个 HTTP GET 请求来访问此 URL:

http://localhost.:51149/Home/TestMe

返回了预期的全页内容。

然后,我将请求更改为使用 HTTP HEAD 而不是 HTTP GET。我在原始输出中只收到了预期的头部信息,没有主体信息。

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Wed, 07 Jul 2010 16:58:55 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 2.0
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 1120
Connection: Close

我的猜测是,您对操作方法添加了约束,使其仅响应 HTTP GET 动词。如果您执行类似的操作,它将适用于 GETHEAD,或者如果它不提供任何值,您可以完全忽略该约束。

public class HomeController : Controller
{
    [AcceptVerbs(new[] {"GET", "HEAD"})]
    public ActionResult TestMe()
    {
        return View();
    }
}

I created a simple action method in an ASP.Net MVC 2 project:

public class HomeController : Controller
{
    public ActionResult TestMe()
    {
        return View();
    }
}

Then I launched Fiddler and built up an HTTP GET request to hit this URL:

http://localhost.:51149/Home/TestMe

The expected full page content was returned.

Then, I changed the request to use an HTTP HEAD instead of an HTTP GET. I received just the expected head info and no body info in the raw output.

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Wed, 07 Jul 2010 16:58:55 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 2.0
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 1120
Connection: Close

My guess is that you are including a constraint on the action method such that it will only respond to HTTP GET verbs. If you do something like this, it will work for both GET and HEAD, or you can omit the constraint entirely if it provides no value.

public class HomeController : Controller
{
    [AcceptVerbs(new[] {"GET", "HEAD"})]
    public ActionResult TestMe()
    {
        return View();
    }
}
⒈起吃苦の倖褔 2024-09-15 03:05:57

您只需执行以下操作即可获得结果

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
public ActionResult TestMe() =>View();

You can achieve the result by simply doing following

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
public ActionResult TestMe() =>View();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文