我具有如下的MVC方法,该方法支持[HttpGet]
属性。
[HttpGet]
[ActionName("Index")]
public ActionResult IndexGet(string l)
{
return View();
}
但是,我注意到我们的Web服务器日志文件中收到了HEAD
请求,但是由于我没有定义[HttpHead]
,因此我们返回的是404。 Web服务器日志文件条目例如是
2020-05-20 00:00:51 W3SVC1 EC2AMAZ 10.0.0.206 HEAD / 443-XXX.XXX.XXX.XXX HTTP / 1.1Mozilla / 5.0 +(兼容; + UptimeRobot / 2.0; + http://www.uptimerobot.com/)-www.domainname.com 404 0 0 306 694 155
和
2020-05-20 00:01:13 W3SVC1 EC2AMAZ 10.0.0.206 GET / 443-XXX.XXX.XXX.XXX HTTP / 1.1Mozilla / 5.0 +(兼容; + UptimeRobot / 2.0; + http://www.uptimerobot.com/)-www.domainname.com 200 0 0 156847 674 1065
如何添加对[HttpHead]
的支持以及响应中应返回的内容。我尝试同时添加[HttpHead]
和[HttpGet]
[HttpHead]
[HttpGet]
[ActionName("Index")]
public ActionResult IndexGet(string l)
{
return View();
}
但是在浏览器中获得以下内容
找不到资源。说明:HTTP404。资源正在寻找(或其依赖项之一)已被删除,更名,或暂时不可用。请查阅以下网址,并确保其拼写正确。
并且它破坏了[HttpGet]
方法。如果我删除[HttpHead]
并刷新URL,则[HttpGet]
可以正常加载。
HEAD请求应仅响应标头,而不响应内容。因此,不应与GET混合使用。
最简单的选择是在控制器中创建一个额外的方法来处理该HEAD请求:
[HttpHead]
[ActionName("Index")]
public ActionResult IsAlive()
{
return Ok();
}
尝试使用AcceptVerbs属性代码,而不使用HttpHead和HttpGet
[AcceptVerbs(new[] { "GET", "HEAD" })]
[ActionName("Index")]
public ActionResult IndexGet(string l)
{
return View();
}