2

While I realise I can add a generic route (SO Post) to redirect all HEAD requests to a central controller. This is not the end of the story because then I would need to validate that the path matches a controller action and is not a 404 etc. This all adds overhead and seems to me should rather be done inside the actions themselves - see below.

However what I want to do is that if one of my controller methods is hit I would like it to generically return a 200 viz:

public virtual ActionResult SomeActionMethod()
{
    if (Request.RequestType == "HEAD")
        return new HttpStatusCodeResult(HttpStatusCode.OK);

    //Other code here that does things like DB calls etc. etc.
}

So instead of having to add the if test everywhere how do I generically for a HEAD:

  1. Respond with a HttpStatusCode.OK for all of my action methods
  2. Optionally turn off 1. when I want to do some additional processing before deciding whether to return HttpStatusCode.OK
TheEdge
  • 9,291
  • 15
  • 67
  • 135
  • 1
    Search term is "ASP.Net MVC action filter"... Side note: are you sure you actually need to respond to `HEAD`? Especially if you plan to mostly lie anyway by not returning anything useful? – Alexei Levenkov Aug 27 '19 at 01:56
  • Need to respond because crawlers come looking (based on sitemap etc.), status checks and so on. – TheEdge Aug 27 '19 at 02:02
  • Not lying either - its that the actions process logic which is irrelevant to actually returning whether the request was successful or not as no body will be returned. Doing the action code slows down the website for no gain. – TheEdge Aug 27 '19 at 02:13
  • Make sure it actually useful... "looking" and "impacting SEO" are different things... (HEAD for https://www.microsoft.com/en-us/p/test/9296331ZWX57 returns 200... just saying) – Alexei Levenkov Aug 27 '19 at 02:14

0 Answers0