2017-03-15 4 views
0

スニペットの下に1.1URIのQueryString JsonSerializerSettingsとAsp.Netコア

入力/出力のために正常に動作している(ボディ)形式

// ConfigureServices

.AddJsonOptions(jsonOption => 
{ 
    jsonOption.SerializerSettings.ContractResolver = new DefaultContractResolver() 
    { 
    NamingStrategy = new SnakeCaseNamingStrategy(true, true) 
    }; 
}) 

クエリ文字列についても同様の動作

/URL/GetDataの?SYSTEM_NAME =インテル& is_active =真

アピ

public class SystemController : Controller 
{ 
    public List<string> GetData([FromQuery]string SystemName, FromQuery]bool IsActive) 
    { 
     Assert.Equals("intel", SystemName); 
     Assert.Equals(true, IsActive); 
     return null; 
    } 
} 

私はバックキャメルケースの文字列モデルバインドを照会することができますどのように任意の提案。アドバンス

で おかげでまた、あなたはそれ以上の情報

答えて

0

が必要な場合は私に知らせてください:)

using System; 
using System.Globalization; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore.Http.Internal; 
using Microsoft.AspNetCore.Mvc.ModelBinding; 

namespace Application.ValueProviders 
{ 
    public class CamelCaseQueryStringValueProviderFactory : IValueProviderFactory 
    { 
     public Task CreateValueProviderAsync(ValueProviderFactoryContext context) 
     { 
      if (context == null) 
       throw new ArgumentNullException(nameof(context)); 
      return AddValueProviderAsync(context); 
     } 

     private async Task AddValueProviderAsync(ValueProviderFactoryContext context) 
     { 
      var collection = context.ActionContext.HttpContext.Request.Query 
       .ToDictionary(t => ToCamelCaseFromSnakeCase(t.Key), t => t.Value, StringComparer.OrdinalIgnoreCase); 

      var valueProvider = new QueryStringValueProvider(
       BindingSource.Query, 
       new QueryCollection(collection), 
       CultureInfo.InvariantCulture); 

      context.ValueProviders.Add(valueProvider); 
     } 

     private string ToCamelCaseFromSnakeCase(string str) 
     { 
      return str.Split(new[] { "_" }, 
       StringSplitOptions.RemoveEmptyEntries). 
       Select(s => char.ToUpperInvariant(s[0]) + s.Substring(1, s.Length - 1)). 
       Aggregate(string.Empty, (s1, s2) => s1 + s2); 
     } 
    } 
}