Skip to content

Instantly share code, notes, and snippets.

@akunzai
Created June 23, 2015 14:11
Show Gist options
  • Save akunzai/1a7be61204c27699ab7a to your computer and use it in GitHub Desktop.
Save akunzai/1a7be61204c27699ab7a to your computer and use it in GitHub Desktop.
Extends the HttpRequestMessage collection
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
namespace System.Web.Http {
/// <summary>
/// Extends the HttpRequestMessage collection
/// </summary>
/// <remarks>http://weblog.west-wind.com/posts/2013/Apr/15/WebAPI-Getting-Headers-QueryString-and-Cookie-Values</remarks>
public static class HttpRequestMessageExtensions {
/// <summary>
/// Returns a dictionary of QueryStrings that's easier to work with
/// than GetQueryNameValuePairs KevValuePairs collection.
///
/// If you need to pull a few single values use GetQueryString instead.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static Dictionary<string, string> GetQueryStrings(this HttpRequestMessage request) {
return request.GetQueryNameValuePairs()
.ToDictionary(kv => kv.Key, kv => kv.Value, StringComparer.OrdinalIgnoreCase);
}
/// <summary>
/// Returns raw string of QueryStrings
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static string GetRawQueryString(this HttpRequestMessage request) {
var items = new List<string>();
request.GetQueryNameValuePairs().ToList().ForEach(it => {
items.Add(string.Concat(it.Key, "=", Uri.EscapeDataString(it.Value)));
});
return items.Count == 0 ? string.Empty : string.Join("&", items.ToArray());
}
/// <summary>
/// Returns an individual querystring value
/// </summary>
/// <param name="request"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetQueryString(this HttpRequestMessage request, string key) {
// IEnumerable<KeyValuePair<string,string>> - right!
var queryStrings = request.GetQueryNameValuePairs();
if (queryStrings == null)
return null;
var match = queryStrings.FirstOrDefault(kv => string.Compare(kv.Key, key, true) == 0);
if (string.IsNullOrEmpty(match.Value))
return null;
return match.Value;
}
/// <summary>
/// Returns an individual HTTP Header value
/// </summary>
/// <param name="request"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetHeader(this HttpRequestMessage request, string key) {
IEnumerable<string> keys = null;
if (!request.Headers.TryGetValues(key, out keys))
return null;
return keys.First();
}
/// <summary>
/// Retrieves an individual cookie from the cookies collection
/// </summary>
/// <param name="request"></param>
/// <param name="cookieName"></param>
/// <returns></returns>
public static string GetCookie(this HttpRequestMessage request, string cookieName) {
CookieHeaderValue cookie = request.Headers.GetCookies(cookieName).FirstOrDefault();
if (cookie != null)
return cookie[cookieName].Value;
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment