The Web API Post([FromBody]MyClass value) Controller Post([FromBody]MyClass value) takes as input a class such as:

 MyClass { public string Name {get;set;} public byte[] Data {get;set;} } 

What means from the C # client application can I send the same structure?

    1 answer 1

    I suggest looking in the direction of HttpClient .
    You also did not specify the received ContentType , so I suggest using json and serializing the object being sent using Newtonsoft.JSON :

    As a result, you can send a request and receive a response like this:

     public async Task<WebApiResponse> PostAsync(string address, object data) { try { using (var httpClient = new HttpClient()) { var dataString = JsonConvert.SerializeObject(data); var httpContent = new StringContent(dataString, Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync(address, httpContent); var responseDataString = await response.Content.ReadAsStringAsync(); var webApiResponse = new WebApiResponse(response.StatusCode, responseDataString); return webApiResponse; } } catch (HttpRequestException ex) { const HttpStatusCode errorStatusCode = (HttpStatusCode)599; var webApiResponse = new WebApiResponse(errorStatusCode, ex.Message); return webApiResponse; } } 

    where WebApiResponse is a class that contains the received answer:

     public class WebApiResponse { public bool IsSuccessful => (int)StatusCode >= 200 && (int)StatusCode <= 399; public HttpStatusCode StatusCode { get; } public string ResponseDataString { get; } public WebApiResponse(HttpStatusCode statusCode, string responseDataString) { StatusCode = statusCode; ResponseDataString = responseDataString; } } 
    • I didn’t clarify about ContentType, because in this case I don’t know anything about it. I simply created the MVC Web API project and declared the above class there with a string and an array of bytes. If the class had only string properties, it would be possible to send data from the client through the WebClient and NameValueCollection. However, no conversion to JSON occurs explicitly in the code. I thought that there is some regular way in .Net, without involving third-party libraries. - Pustota