简体   繁体   中英

How to post and cast payload?

My goal is to call api (via post), accept payload as base type and later cast it to concrete type. If I do that from main solution (where my api stands), everything works well. But I can't understand why same code doesn't work from other solutions.

So I have my request (declared in different solutions)

namespace Nb
{
    public class NbRequestBase
    {
        public string BaseProp { get; set; }
    }
    public class NbRequestConcrete : NbRequestBase
    {
        public string ConcreteProp { get; set; }
    }
}

And this is my endpoint:

[HttpPost]
[Route("payments/nb")]
public IHttpActionResult Prepare(NbRequestBase request)
{
    if(request is NbRequestConcrete)
    {

    }

    try
    {
        // <<< INSERT CODE HERE >>>
        NbRequestConcrete nbRequestConcrete = (NbRequestConcrete)request;

        return Ok();
    }
    catch (Exception ex)
    {
        _logger.Error(ex);

        return InternalServerError();
    }
}

and this is my calling code:

    NbRequestConcrete requestTwo = new NbRequestConcrete()
    {
        BaseProp = "BaseProp",
        ConcreteProp = "ConcreteProp"
    };

    using (var client = new HttpClient())
    {
        var _clientId = "_clientId";
        var _clientSecret = "_clientSecret";

        client.BaseAddress = new Uri("http://localhost:50228");

        #region Formatter

        JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter();
        formatter.SerializerSettings.TypeNameHandling = TypeNameHandling.All;

        List<MediaTypeFormatter> formatters = new List<MediaTypeFormatter>();
        formatters.Add(formatter);

        #endregion

        var responseMessage = client.PostAsync($"payments/nb?clientId={_clientId}&clientSecret={_clientSecret}", requestTwo, formatter).Result;
        responseMessage.EnsureSuccessStatusCode();
    }

If I put my calling code into other project/solution (for example just new console app), API endpoint is hit, but payload is null.

payload when called form console app

If I put exacly same calling code into project where my api is (for example in same API endpoint method, at try/catch block start and call it again), API endpoint is hit, payload is NOT null and casting works. Why is it? And how to fix it?

payload when called from same solution try/catch start

And BTW. How to make this call via postman?

Regards

This line tells the model binder to set the values of any matching properties in request to the value that was passed to the API:

public IHttpActionResult Prepare(NbRequestBase request)

The model binder does not attach all the other properties to the request, because it has no idea what they would be.

Problem was Assemblies name where NbRequestConcrete in console app lived in one assembly and on API lived in other. So request was different.

{
  "$type": "Nb.NbRequestConcrete, Tester",
  "ConcreteProp": "ConcreteProp",
  "BaseProp": "BaseProp"
}

VS

{
  "$type": "Nb.NbRequestConcrete, MYApi",
  "ConcreteProp": "ConcreteProp",
  "BaseProp": "BaseProp"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM