final update
This commit is contained in:
28
Website/LOC.Core/JsonModelBinder.cs
Normal file
28
Website/LOC.Core/JsonModelBinder.cs
Normal file
@ -0,0 +1,28 @@
|
||||
namespace LOC.Core
|
||||
{
|
||||
using System.IO;
|
||||
using System.Web.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
public class JsonModelBinder : DefaultModelBinder
|
||||
{
|
||||
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
|
||||
{
|
||||
if (!IsJsonRequest(controllerContext))
|
||||
return base.BindModel(controllerContext, bindingContext);
|
||||
var request = controllerContext.HttpContext.Request;
|
||||
var stream = request.InputStream;
|
||||
if (stream.Length == 0)
|
||||
return null;
|
||||
stream.Position = 0;
|
||||
var jsonStringData = new StreamReader(stream).ReadToEnd();
|
||||
return JsonConvert.DeserializeObject(jsonStringData, bindingContext.ModelType);
|
||||
}
|
||||
|
||||
private static bool IsJsonRequest(ControllerContext controllerContext)
|
||||
{
|
||||
var contentType = controllerContext.HttpContext.Request.ContentType;
|
||||
return contentType.Contains("application/json");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user