Asp.net mvc 如何在ASP.NET MVC ValueProvider中使用ServiceStack.Text进行Json反序列化
在控制器方法参数的值绑定期间,如何使用ServiceStack.Text Json序列化程序对ASP.NET MVC请求中的字符串进行反序列化?我最终不得不编写自定义类。要使用下面的工厂,请使用以下内容更新您的Asp.net mvc 如何在ASP.NET MVC ValueProvider中使用ServiceStack.Text进行Json反序列化,asp.net-mvc,json,
servicestack-text,value-provider,Asp.net Mvc,Json,
servicestack Text,Value Provider,在控制器方法参数的值绑定期间,如何使用ServiceStack.Text Json序列化程序对ASP.NET MVC请求中的字符串进行反序列化?我最终不得不编写自定义类。要使用下面的工厂,请使用以下内容更新您的应用程序\u Start: ValueProviderFactories.Factories.Remove( ValueProviderFactories.Factories.OfType< JsonValueProviderFactory >().FirstOrDefault
应用程序\u Start
:
ValueProviderFactories.Factories.Remove( ValueProviderFactories.Factories.OfType< JsonValueProviderFactory >().FirstOrDefault() );
ValueProviderFactories.Factories.Add( new JsonServiceStackValueProviderFactory() )
valueProviderFactorys.Factories.Remove(valueProviderFactorys.Factories.OfType().FirstOrDefault());
valueProviderFactorys.Factories.Add(新JsonServiceStackValueProviderFactory())
以下是对我有效的方法:
public sealed class JsonServiceStackValueProviderFactory: ValueProviderFactory
{
public override IValueProvider GetValueProvider( ControllerContext controllerContext )
{
if( controllerContext == null )
throw new ArgumentNullException( "controllerContext" );
var jsonData = GetDeserializedObject( controllerContext );
if( jsonData == null )
return null;
var backingStore = new Dictionary< string, object >( StringComparer.OrdinalIgnoreCase );
AddToBackingStore( backingStore, String.Empty, jsonData );
return new DictionaryValueProvider< object >( backingStore, CultureInfo.CurrentCulture );
}
private static object GetDeserializedObject( ControllerContext controllerContext )
{
if( !controllerContext.HttpContext.Request.ContentType.StartsWith( "application/json", StringComparison.OrdinalIgnoreCase ) )
{
// not JSON request
return null;
}
var reader = new StreamReader( controllerContext.HttpContext.Request.InputStream );
var bodyText = reader.ReadToEnd();
if( String.IsNullOrEmpty( bodyText ) )
{
// no JSON data
return null;
}
var firstNonEmptyChar = GetFirstNonEmptyChar( bodyText );
if( firstNonEmptyChar == '[' )
{
var jsonData = JsonSerializer.DeserializeFromString< List< Dictionary< string, object > > >( bodyText );
return jsonData;
}
else
{
var jsonData = JsonSerializer.DeserializeFromString< Dictionary< string, object > >( bodyText );
return jsonData;
}
}
private static void AddToBackingStore( Dictionary< string, object > backingStore, string prefix, object value )
{
var d = value as IDictionary< string, object >;
if( d != null )
{
foreach( var entry in d )
{
AddToBackingStore( backingStore, MakePropertyKey( prefix, entry.Key ), entry.Value );
}
return;
}
var l = value as IList;
if( l != null )
{
for( var i = 0; i < l.Count; i++ )
{
AddToBackingStore( backingStore, MakeArrayKey( prefix, i ), l[ i ] );
}
return;
}
// primitive
backingStore[ prefix ] = value;
}
private static string MakeArrayKey( string prefix, int index )
{
return prefix + "[" + index.ToString( CultureInfo.InvariantCulture ) + "]";
}
private static string MakePropertyKey( string prefix, string propertyName )
{
return ( String.IsNullOrEmpty( prefix ) ) ? propertyName : prefix + "." + propertyName;
}
private static char? GetFirstNonEmptyChar( string @string )
{
for( var i = 0; i < @string.Length; i++ )
{
if( !char.IsWhiteSpace( @string[ i ] ) )
return @string[ i ];
}
return new char?();
}
}
公共密封类JsonServiceStackValueProviderFactory:ValueProviderFactory
{
公共覆盖IValueProvider GetValueProvider(ControllerContext ControllerContext)
{
如果(controllerContext==null)
抛出新ArgumentNullException(“controllerContext”);
var jsonData=GetDeserializedObject(controllerContext);
if(jsonData==null)
返回null;
var backingStore=newdictionary(StringComparer.OrdinalIgnoreCase);
AddToBackStore(backingStore,String.Empty,jsonData);
返回新的DictionaryValueProvider