Created
October 22, 2014 00:40
-
-
Save randyburden/42bc688780564ed01107 to your computer and use it in GitHub Desktop.
OwinRequest extensions for getting query string, form body, and header parameters as Dictionary<string,string>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Owin Request extensions. | |
/// </summary> | |
public static class OwinRequestExtensions | |
{ | |
/// <summary> | |
/// Gets the combined request parameters from the form body, query string, and request headers. | |
/// </summary> | |
/// <param name="request">Owin request.</param> | |
/// <returns>Dictionary of combined form body, query string, and request headers.</returns> | |
public static Dictionary<string, string> GetRequestParameters( this IOwinRequest request ) | |
{ | |
var bodyParameters = request.GetBodyParameters(); | |
var queryParameters = request.GetQueryParameters(); | |
var headerParameters = request.GetHeaderParameters(); | |
bodyParameters.Merge( queryParameters ); | |
bodyParameters.Merge( headerParameters ); | |
return bodyParameters; | |
} | |
/// <summary> | |
/// Gets the query string request parameters. | |
/// </summary> | |
/// <param name="request">Owin Request.</param> | |
/// <returns>Dictionary of query string parameters.</returns> | |
public static Dictionary<string, string> GetQueryParameters( this IOwinRequest request ) | |
{ | |
var dictionary = new Dictionary<string, string>( StringComparer.CurrentCultureIgnoreCase ); | |
foreach( var pair in request.Query ) | |
{ | |
var value = GetJoinedValue( pair.Value ); | |
dictionary.Add( pair.Key, value ); | |
} | |
return dictionary; | |
} | |
/// <summary> | |
/// Gets the form body request parameters. | |
/// </summary> | |
/// <param name="request">Owin Request.</param> | |
/// <returns>Dictionary of form body parameters.</returns> | |
public static Dictionary<string, string> GetBodyParameters( this IOwinRequest request ) | |
{ | |
var dictionary = new Dictionary<string, string>( StringComparer.CurrentCultureIgnoreCase ); | |
var formCollectionTask = request.ReadFormAsync(); | |
formCollectionTask.Wait(); | |
foreach( var pair in formCollectionTask.Result ) | |
{ | |
var value = GetJoinedValue( pair.Value ); | |
dictionary.Add( pair.Key, value ); | |
} | |
return dictionary; | |
} | |
/// <summary> | |
/// Gets the header request parameters. | |
/// </summary> | |
/// <param name="request">Owin Request.</param> | |
/// <returns>Dictionary of header parameters.</returns> | |
public static Dictionary<string, string> GetHeaderParameters( this IOwinRequest request ) | |
{ | |
var dictionary = new Dictionary<string, string>( StringComparer.CurrentCultureIgnoreCase ); | |
foreach( var pair in request.Headers ) | |
{ | |
var value = GetJoinedValue( pair.Value ); | |
dictionary.Add( pair.Key, value ); | |
} | |
return dictionary; | |
} | |
private static string GetJoinedValue( string[] value ) | |
{ | |
if( value != null ) | |
return string.Join( ",", value ); | |
return null; | |
} | |
} |
Yeah, ditto what @RJCuthbertson said... No "Merge" in this code
Add this to your code to get Merge functionality
///
/// Merges the current data to a new data
///
///
public static void Merge<TKey, TValue>(this IDictionary<TKey, TValue> to, IDictionary<TKey, TValue> data)
{
foreach (var item in data)
{
if (to.ContainsKey(item.Key) == false)
{
to.Add(item.Key, item.Value);
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your
Dictionary<TKey, TValue>
Merge extension is missing - does this override values in the request body with those in the query string or headers if there are duplicate keys?