我想为我的MVC .NET应用程序做一个Json Post请求api beeing:http://localhost:39622/api/holiday
Json文件应代表以下类:
public class Holiday
{
public string ldapName { get; set; }
public DateTime date {get; set;}
}
我已经在“postHoliday”方法中初始化了我的模型类的实例,并将其转换为JSON文件。
public void postHoliday()
{
Holiday holiday1 = new Holiday();
holiday1.ldapName = "dkassube";
holiday1.date = new DateTime(2016, 08, 01);
string output = JsonConvert.SerializeObject(holiday1);
WebRequest request = WebRequest.Create("localhost:39622/api/holiday");
request.Method = "POST";
// What to do here?
}
我不确定如何将JSON发送到我的API控制器以及如何处理请求到达时。当API控制器收到JSON时,我想将其转换回Holiday的实例。
public class HolidayController : ApiController
{
public Holiday handleIncomingHoliday()
{
Holiday incomingHoliday = new Holiday();
// What to do here?
return incomingHoliday;
}
}