So I have been struggling to get this work for some time now. Let me explain the requirement. I have to write a WebAPI that will accept an XML, do some look up, and return a response.
I am new to this so sought some help. The suggestion was to create a custom object that represents the incoming XML and use that object as the param for the method exposed by the WebAPI. The result will be a class that's not a problem.
Here are the steps done.
Created an empty WebAPI project. Added a class that represents the incoming XML.
Incoming XML:
<InComingStudent>
<StudentID>10</StudentID>
<Batch>56</Batch>
</InComingStudent>
The class:
public class InComingStudent
{
public string StudentID { get; set; }
public string Batch { get; set; }
}
Return object of this type:
public class StudentResult
{
public string StudentID { get; set; }
public string Batch { get; set; }
public string Score { get; set; }
}
Added a Students controller with this method:
public class StudentsController : ApiController
{
[HttpPost]
public StudentResult StudentStatus(InComingStudent inComingStudent)
{
ProcessStudent po = new ProcessStudent();
StudentResult studentResult = po.ProcessStudent();
return StudentResult;
}
}
Ran the service. It opened in a new browser with 404. That's ok as I don't have a starting page.
Wrote a console app to test:
private static async void PostToStudentService()
{
using (var client = new HttpClient())
{
var studentToPost = new InComingStudent() { StudentID = "847", Batch="56"};
client.BaseAddress = new Uri("http://localhost:53247/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
HttpResponseMessage response = await client.PostAsXmlAsync("api/Students/StudentStatus", studentToPost);
if (response.IsSuccessStatusCode)
{
// Print the response
}
}
}
MediaTypeWithQualityHeaderValue is set to application/xml. So far so good. When the service is running and I run the console app, the response is 404 "Not Found".
What am I missing?
Any help is greatly appreciated.
Regards.