2016-11-12 81 views
2

我試圖抓住「狀態」和「所有」鍵,來自請求的URL的值,並且無法弄清楚如何構建我的類對象。dotnet核心webapi json-api兼容查詢字符串路由

我所指的JSON API規範可以在這裏找到: http://jsonapi.org/recommendations/#filtering

// requested url 
/api/endpoint?filter[status]=all 


// my attempt at model binding 
public class FilterParams 
{ 
    public Dictionary<string, string> Filter { get; set; } 
} 

[HttpGet] 
public string Get([FromUri] FilterParams filter) 
{ 
    // never gets populated... 
    var filterStatus = filter.Filter["status"]; 
} 

回答

0
  1. 您可以使用該IModelBinder

    • 定義模型綁定:

      public class FilterParamsModelBinder : IModelBinder 
      { 
          public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) 
          { 
           if (bindingContext.ModelType != typeof(FilterParams)) return false; 
      
           Dictionary<string, string> result = new Dictionary<string, string>(); 
      
           var parameters = actionContext.Request.RequestUri.Query.Substring(1); 
      
           if(parameters.Length == 0) return false; 
      
           var regex = new Regex(@"filter\[(?<key>[\w]+)\]=(?<value>[\w^,]+)"); 
      
           parameters 
            .Split('&') 
            .ToList() 
            .ForEach(_ => 
            { 
             var groups = regex.Match(_).Groups; 
      
             if(groups.Count == 0) 
              bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Cannot convert value."); 
      
             result.Add(groups["key"].Value, groups["value"].Value); 
            }); 
      
           bindingContext.Model = new FilterParams { Filter = result}; 
      
           return bindingContext.ModelState.IsValid; 
          } 
      } 
      
    • 使用它:

      [HttpGet] 
      public string Get([ModelBinderAttribute(typeof(FilterParamsModelBinder))] FilterParams filter) 
      { 
          //your code 
      } 
      
  2. 如果你可以定義一個路線,如 「/ API /終點過濾=狀態,所有?」,而不是,不是,你可以使用一個TypeConverter

    • 定義轉換器:

      public class FilterConverter : TypeConverter 
      { 
          public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 
          { 
           if (!(value is string)) return base.ConvertFrom(context, culture, value); 
      
           var keyValue = ((string)value).Split(','); 
      
           return new FilterParams 
           { 
            Filter = new Dictionary<string, string> { [keyValue[0]] = keyValue[1] } 
           }; 
          } 
      
          public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
          { 
           return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); 
          }    
      } 
      
    • 使用它:

      [TypeConverter(typeof(FilterConverter))] 
      public class FilterParams 
      { 
          public Dictionary<string, string> Filter { get; set; } 
      } 
      
      [HttpGet] 
      public string Get(FilterParams filter) 
      { 
          var filterStatus = filter.Filter["status"]; 
      } 
      
+0

不,我想堅持到位於JSON API標準:http://jsonapi.org/recommendations/#filtering –

+0

@CarlSagan增加了一個可選的解決方案使用模型綁定器。 –