SkyeBoniwell
SkyeBoniwell

Reputation: 7092

How to use HttpClient to connect to an API that is expecting a custom type?

I have a .net controller that accepts POST events. It is expecting 2 dates from the client system(c# console app).

How can I connect to this controller and send the needed data if the console app does not know anything about the ContestDates class?

Here is the controller:

public class ContestDates
{
    public DateTime FirstDate { get; set; }
    public DateTime SecondDate { get; set; }
}


[HttpPost("api/contestDates/random")]
public IActionResult PostRandomDates(ContestDates contestDates)
{
    //do stuff with contestDates...
    return Ok();
}

Thanks!

Upvotes: 0

Views: 56

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

One possibility is to declare the same type in the client application. Another possibility is to move this type into a Contracts assembly that you could share between your Web project and your client project (this could be the preferred approach if you have many and complex objects to pass between your client and web API).

Upvotes: 1

Related Questions