Just been stuck here for long time now. Thought someone might be able to put me in right direction. I have created a ASP.NET Web API 2 application. There is one function which will return all ships. Now I want to receive an xml inside my controller function. How can I achieve that. Below is my HttpClient calling code which is giving me Bad Request error. Can someone guide me in right direction to how to pass xml to Web api function.
Routing
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Controller Action
public IHttpActionResult GetSummaryFunction(string id)
{
return Ok(id);
}
Client Code
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:53633/");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/xml"));
string testXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SummaryRQ xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/Test.WebAPI.Controllers\" Version=\"1.00\" Target=\"Test\"><POS><Source Login=\"test\" Password=\"test\"></Source></POS></SummaryRQ>";
var url = "api/summaryfunction/" + testXml;
HttpResponseMessage response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
}
else
{
}