// http://blog.duc.as/2011/06/07/making-mvc-3-a-little-more-dynamic/ using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.IO; using System.Web.Helpers; using HeuristicLab.Services.Optimization.ControllerService.Model; using HeuristicLab.Services.Optimization.ControllerService.Parsers; namespace Mvc3TestApplication.Binders { public class ExperimentJsonAttribute : CustomModelBinderAttribute { public override IModelBinder GetBinder() { return new ExperimentJsonBinder(); } } public class ExperimentJsonBinder : IModelBinder { public ExperimentJsonBinder() { } public object BindModel(ControllerContext controllerCtx, ModelBindingContext bindingCtx) { var contentType = controllerCtx.HttpContext.Request.ContentType; if (!contentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)) return null; string body; using (var stream = controllerCtx.HttpContext.Request.InputStream) { stream.Seek(0, System.IO.SeekOrigin.Begin); using (var reader = new StreamReader(stream)) body = reader.ReadToEnd(); } if (string.IsNullOrEmpty(body)) return null; return AlgorithmConverter.ConvertJsonToExperiment(body); } } }