Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OwinRequest extensions for getting query string, form body, and header parameters as Dictionary<string,string>
/// <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;
}
}
@RJCuthbertson

This comment has been minimized.

Show comment
Hide comment
@RJCuthbertson

RJCuthbertson Jun 22, 2016

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?

RJCuthbertson commented Jun 22, 2016

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?

@bkwdesign

This comment has been minimized.

Show comment
Hide comment
@bkwdesign

bkwdesign Aug 27, 2017

Yeah, ditto what @RJCuthbertson said... No "Merge" in this code

bkwdesign commented Aug 27, 2017

Yeah, ditto what @RJCuthbertson said... No "Merge" in this code

@tallalkazmi

This comment has been minimized.

Show comment
Hide comment
@tallalkazmi

tallalkazmi Mar 29, 2018

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);
}
}
}

tallalkazmi commented Mar 29, 2018

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