Use Json.Net for MVC ValueProviderFactory
This commit is contained in:
@@ -15,7 +15,6 @@
|
||||
<TargetFrameworkProfile />
|
||||
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
|
||||
<RestorePackages>true</RestorePackages>
|
||||
<AssemblyVersion>2.2.16272.1003</AssemblyVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@@ -104,6 +103,7 @@
|
||||
<Compile Include="DataModelExtension\DocumentTemplateExtensions.cs" />
|
||||
<Compile Include="DataModelExtension\JobSubTypeExtensions.cs" />
|
||||
<Compile Include="DataModelExtension\JobTypeExtensions.cs" />
|
||||
<Compile Include="MvcExtensions\JsonNet\JsonDotNetValueProviderFactory.cs" />
|
||||
<Compile Include="MvcExtensions\JsonNet\JsonNetResult.cs" />
|
||||
<Compile Include="MvcExtensions\PartialCompiled\PartialCompiledHtmlExtensions.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using System;
|
||||
using System.Dynamic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Disco.Web.Extensions
|
||||
{
|
||||
public class JsonDotNetValueProviderFactory : ValueProviderFactory
|
||||
{
|
||||
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
|
||||
{
|
||||
if (controllerContext == null)
|
||||
throw new ArgumentNullException(nameof(controllerContext));
|
||||
|
||||
if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
|
||||
return null;
|
||||
|
||||
if (controllerContext.HttpContext.Request.InputStream.Length == 0)
|
||||
return null;
|
||||
|
||||
var jsonSerializer = new JsonSerializer();
|
||||
jsonSerializer.Converters.Add(new ExpandoObjectConverter());
|
||||
|
||||
using (var streamReader = new StreamReader(controllerContext.HttpContext.Request.InputStream, Encoding.UTF8, true, 0x400, true))
|
||||
{
|
||||
using (var jsonReader = new JsonTextReader(streamReader))
|
||||
{
|
||||
var bodyObject = jsonSerializer.Deserialize<ExpandoObject>(jsonReader);
|
||||
return new DictionaryValueProvider<object>(bodyObject, CultureInfo.CurrentCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user