1 | // http://blog.duc.as/2011/06/07/making-mvc-3-a-little-more-dynamic/
|
---|
2 |
|
---|
3 | using System;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Linq;
|
---|
6 | using System.Web;
|
---|
7 | using System.Web.Mvc;
|
---|
8 | using System.IO;
|
---|
9 | using System.Web.Helpers;
|
---|
10 | using HeuristicLab.Services.Optimization.ControllerService.Model;
|
---|
11 | using HeuristicLab.Services.Optimization.ControllerService.Parsers;
|
---|
12 |
|
---|
13 | namespace Mvc3TestApplication.Binders
|
---|
14 | {
|
---|
15 | public class AlgorithmJsonAttribute : CustomModelBinderAttribute
|
---|
16 | {
|
---|
17 | public override IModelBinder GetBinder()
|
---|
18 | {
|
---|
19 | return new AlgorithmJsonBinder();
|
---|
20 | }
|
---|
21 | }
|
---|
22 |
|
---|
23 | public class AlgorithmJsonBinder : IModelBinder
|
---|
24 | {
|
---|
25 | public AlgorithmJsonBinder()
|
---|
26 | {
|
---|
27 | }
|
---|
28 |
|
---|
29 | public object BindModel(ControllerContext controllerCtx, ModelBindingContext bindingCtx)
|
---|
30 | {
|
---|
31 | var contentType = controllerCtx.HttpContext.Request.ContentType;
|
---|
32 | if (!contentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
|
---|
33 | return null;
|
---|
34 |
|
---|
35 | string body;
|
---|
36 | using (var stream = controllerCtx.HttpContext.Request.InputStream)
|
---|
37 | {
|
---|
38 | stream.Seek(0, System.IO.SeekOrigin.Begin);
|
---|
39 | using (var reader = new StreamReader(stream))
|
---|
40 | body = reader.ReadToEnd();
|
---|
41 | }
|
---|
42 | if (string.IsNullOrEmpty(body)) return null;
|
---|
43 | return AlgorithmConverter.Convert(body);
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | }
|
---|
48 |
|
---|
49 | public class ExperimentJsonAttribute : CustomModelBinderAttribute {
|
---|
50 | public override IModelBinder GetBinder() {
|
---|
51 | return new ExperimentJsonBinder();
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public class ExperimentJsonBinder : IModelBinder {
|
---|
56 | public ExperimentJsonBinder() {
|
---|
57 | }
|
---|
58 |
|
---|
59 | public object BindModel(ControllerContext controllerCtx, ModelBindingContext bindingCtx) {
|
---|
60 | var contentType = controllerCtx.HttpContext.Request.ContentType;
|
---|
61 | if (!contentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
|
---|
62 | return null;
|
---|
63 |
|
---|
64 | string body;
|
---|
65 | using (var stream = controllerCtx.HttpContext.Request.InputStream) {
|
---|
66 | stream.Seek(0, System.IO.SeekOrigin.Begin);
|
---|
67 | using (var reader = new StreamReader(stream))
|
---|
68 | body = reader.ReadToEnd();
|
---|
69 | }
|
---|
70 | if (string.IsNullOrEmpty(body)) return null;
|
---|
71 | return AlgorithmConverter.ConvertExperiment(body);
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | }
|
---|
76 | } |
---|