asp.Netwebapi HttpRequestMessage getとpostのすべてのパラメータを取得

1249 ワード

/// 
///   Http  Get Post    
/// 
/// HttpRequestMessage  
/// Get Post       
public static class HttpRequestMessageExtensions
{
    public static IDictionary GetAllQueryParameters(this HttpRequestMessage request)
    {
        NameValueCollection queryString = HttpContext.Current.Request.QueryString;
        NameValueCollection form = HttpContext.Current.Request.Form;
        IDictionary queryParameters = new Dictionary();
        foreach (string key in queryString)
        {
            if (!queryParameters.ContainsKey(key))
            {
                queryParameters.Add(key, queryString[key]);
            }
        }
        foreach (string key in form)
        {
            if (!queryParameters.ContainsKey(key))
            {
                queryParameters.Add(key, form[key]);
            }
        }
        return queryParameters;
    }
}

使用方法:
public class testController : ApiController
{
    public void Get()
    {
        IDictionary queryParameters = Request.GetAllQueryParameters();
    }
}