Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Web/Binders/AlgorithmJsonAttribute.cs @ 9283

Last change on this file since 9283 was 9227, checked in by fschoepp, 12 years ago

#1888:

  • Experiments will be saved as JSON elements within the blob store.
  • Added simple model and JSON converters.
  • Backend stores and runs experiments.
  • Updated interfaces to save/read experiments.
  • Added a binding to automatically map incoming JSON ajax requests to experiment models.
  • Added the javascript DatatypeMapper to map parameter inputs to the right html elements and vice versa.
  • Added smartwizard to generate Wizards for creating new experiments (New.cshtml).
File size: 2.3 KB
Line 
1// http://blog.duc.as/2011/06/07/making-mvc-3-a-little-more-dynamic/
2
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using System.Web;
7using System.Web.Mvc;
8using System.IO;
9using System.Web.Helpers;
10using HeuristicLab.Services.Optimization.ControllerService.Model;
11using HeuristicLab.Services.Optimization.ControllerService.Parsers;
12
13namespace 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}
Note: See TracBrowser for help on using the repository browser.