Click here to Skip to main content
15,888,208 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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 :
C#
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 BarcodeLayerSettings { Dimensions = new Dimension(200, 200), Contents = "123456789012345678-123456789012345678", PureBarcode = false, Type = BarCodeType.CODE_128 });

    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)
    {
        // determine error here by inspecting httpEx.Message
    }
    return View();
}

and this is my api
C#
[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;

    //var image = Image.FromStream(stream);
    //return image;
}
Posted
Updated 22-Mar-16 7:45am
v2
Comments
John C Rayan 22-Mar-16 6:51am    
Can you debug and see that you are able to get Json String from your object before sending to API.
Duncan Edwards Jones 22-Mar-16 7:56am    
Do you have a data contract attribute on the Defination class?
Nathan Minier 22-Mar-16 8:33am    
JSON.Net, which I assume is being leveraged by HttpClient.PostAsJsonAsync, does not play nicely with interfaces.

I would suggest making an object with concrete properties for the data transfer and cast to it for your data transfer; like a viewmodel, but for the API.

1 solution

Please refer by CP article on how to pass complex model object to WebAPI. It will point you in the right direction.

Call WebAPI's PUT method with complex input type from C#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900