如何将复杂模型对象传递给api [英] How do I can pass the complex model object to api

查看:78
本文介绍了如何将复杂模型对象传递给api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个复杂的模型从我的MVC控制器传递给web api,但是我在api上接收了null模型,任何人都可以帮我将复杂的模型对象传递给web api。这是我的模型。



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 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();
}



这是我的api


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;

    //var image = Image.FromStream(stream);
    //return image;
}

推荐答案

请参阅CP文章,了解如何将复杂模型对象传递给WebAPI。它将指向正确的方向。



使用C#中的复杂输入类型调用WebAPI的PUT方法 [ ^ ]
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#[^]


这篇关于如何将复杂模型对象传递给api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆