Search code examples
asp.net-mvcweb-servicesasp.net-mvc-4api-key

How do I move the API Key from the querystring into the request header?


I've been working through the sample code posted with this article. It works and I could use it as-is. However, I'd really like to submit the apikey as a request header instead of a querystring parameter. This would keep the URL cleaner.

I see how I can just add a request header in a tool like Fiddler but how do I modify the code to look in the headers instead of the querystring?

FYI, I'm using the MVC 4 Beta...


Solution

  • Header variables and URL params are different but normally you'll find them in the same data structure when handling a request. I'm not familiar with .NET, but you should have an array or hash of header input available just like you have of URL params. Something like

    System.Web.HttpContext.Current.Request.Headers["APIKey"];

    eems like a candidate (change the varname)