What's the best way to detect a JSON request on ASP.NET
ajax, asp.net, asp.net-mvc, c#
Solution
Why cant you just pass a bool variable say IsJsonRequest from the client where you are making request?
Then make a check in action method.
or
You could use the request's accept header for this. This indicates what type of content the client wants the server to send to it.
Problem
Most ajax frameworks seem to standardize with "X-Request-With" on either a header or the query string. And in ASP.NET MVC you can use the extension method ``` Request.IsAjaxRequest() ``` Because an ajax client can request several different content types, not just "application/json" ex: "application/xml". I'm using the following code snippet/extension method, but I would love to see what others are doing or if there is something I missed, or a better way. ``` public static bool IsJsonRequest(this HttpRequestBase request) { return request.Headers["Accept"].Split(',') .Any(t => t.Equals("application/json", StringComparison.OrdinalIgnoreCase)); } ``` Thanks in advance.