I want to pass a complex model to the web api from my MVC controller but i am receiving the null model at the api can anybody please help me to pass the complex model object to the web api. Here is my model.
public class Defination
{
public Defination()
{
Layers = new List<ILayerSettings>();
Operations = new List<IOperationSettings>();
}
public bool PreserveExifData { get; set; }
public bool FixGamma { get; set; }
public IList<ILayerSettings> Layers { get; private set; }
public IList<IOperationSettings> Operations { get; private set; }
public int? Quality { get; set; }
public Dimension Dimensions { get; set; }
public ImageFormat Format { get; set; }
}
What I have tried:
this is my controller code :
public ActionResult DynamicImages(string name)
{
Defination def = new Defination { Dimensions = new Dimension(400, 400), FixGamma = true, PreserveExifData = false, Format = ImageFormat.Png };
def.Layers.Add(new ImageLayerSettings { Dimensions = new Dimension(200, 200), URL = "http://www.joshuacasper.com/contents/uploads/joshua-casper-samples-free.jpg", Coordinates = new Coordinates(50, 150), Resize = false });
def.Layers.Add(new BarcodeLayerSettings { Dimensions = new Dimension(200, 200), Contents = "123456789012345678", Type = BarCodeType.CODE_128 });
var tl = new TextLayerSettings { Dimensions = new Dimension(200, 200), Coordinates = new Coordinates(100, 100) };
tl.Strings.Add(new TextString { Brush = "Red", Message = "Testing" });
def.Layers.Add(tl);
def.Operations.Add(new RoundCornersSettings { Radius = 20 });
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:61787/");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsJsonAsync("api/FulfilmentDynamicImage/Generate", def).Result;
response.EnsureSuccessStatusCode();
Task<stream> task = response.Content.ReadAsStreamAsync();
Stream readOnlyStream = task.Result;
Byte[] buffer = new Byte[readOnlyStream.Length];
readOnlyStream.Read(buffer, 0, buffer.Length);
MemoryStream memoryStream = new MemoryStream(buffer);
Image image = Image.FromStream(memoryStream);
image.Debug("FINAL");
}
catch (HttpRequestException httpEx)
{
}
return View();
}
and this is my api
[Route("Generate")]
[HttpPost]
public HttpResponseMessage Generate(Defination def)
{
ImageGenerator imageGenerator = new ImageGenerator();
var stream = imageGenerator.Generate(def);
var img = Image.FromStream(stream);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(ms.ToArray());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
return result;
}