I use two programs: the first one posts an xml-package to an asp.net mvc 4 web api project.
The first application:
const string endpoint = "http://192.168.2.6/WebApi.Test/api/values?format=xml";
var apps = new Apps();
var applications = new List<Application>();
var application = new Application();
application.Key = "key";
application.DisplayName = "name";
application.InstalledVersion = "2012.1.5.42921";
application.Environment = "Prod";
applications.Add(application);
apps.ApplicationsList = applications;
var httpClient = new HttpClient();
var mediaType = new MediaTypeHeaderValue("application/xml");
var requestMessage = new HttpRequestMessage<T>(data, mediaType);
httpClient.PostAsync(endpoint, requestMessage.Content).Result;
The second web api application:
public class ValuesController : ApiController
{
// POST /api/values
public void Post(Apps apps)
{
List<Application> applicationsList = apps.ApplicationsList;
string displayName = applicationsList[0].DisplayName;
string key = applicationsList[0].Key;
string environment = applicationsList[0].Environment;
string hospitalNumber = applicationsList[0].HospitalNumber;
string installedVersion = applicationsList[0].InstalledVersion;
}
}
I also made 2 seperated classes to store my data in each application:
public class Apps
{
[XmlElement("Application")]
public List<Application> ApplicationsList { get; set; }
}
public class Application
{
[XmlAttribute("key")]
public string Key { get; set; }
[XmlAttribute("zhnr")]
public string HospitalNumber { get; set; }
[XmlAttribute("env")]
public string Environment { get; set; }
public string DisplayName { get; set; }
public string InstalledVersion { get; set; }
}
Frederiek
Member
12 Points
2 Posts
ASP.NET MVC 4 beta web api xml post problem
Mar 06, 2012 07:41 PM|LINK
I use two programs: the first one posts an xml-package to an asp.net mvc 4 web api project.
The first application:
const string endpoint = "http://192.168.2.6/WebApi.Test/api/values?format=xml"; var apps = new Apps(); var applications = new List<Application>(); var application = new Application(); application.Key = "key"; application.DisplayName = "name"; application.InstalledVersion = "2012.1.5.42921"; application.Environment = "Prod"; applications.Add(application); apps.ApplicationsList = applications; var httpClient = new HttpClient(); var mediaType = new MediaTypeHeaderValue("application/xml"); var requestMessage = new HttpRequestMessage<T>(data, mediaType); httpClient.PostAsync(endpoint, requestMessage.Content).Result;The second web api application:
public class ValuesController : ApiController { // POST /api/values public void Post(Apps apps) { List<Application> applicationsList = apps.ApplicationsList; string displayName = applicationsList[0].DisplayName; string key = applicationsList[0].Key; string environment = applicationsList[0].Environment; string hospitalNumber = applicationsList[0].HospitalNumber; string installedVersion = applicationsList[0].InstalledVersion; } }I also made 2 seperated classes to store my data in each application:
public class Apps { [XmlElement("Application")] public List<Application> ApplicationsList { get; set; } } public class Application { [XmlAttribute("key")] public string Key { get; set; } [XmlAttribute("zhnr")] public string HospitalNumber { get; set; } [XmlAttribute("env")] public string Environment { get; set; } public string DisplayName { get; set; } public string InstalledVersion { get; set; } }The xml-file captured by Wireshark looks fine:
But the posted object in the ApiController always receives a null-object when a List<Application> is used, can anyone help me?
Frederiek
Member
12 Points
2 Posts
Re: ASP.NET MVC 4 beta web api xml post problem
Mar 07, 2012 11:13 AM|LINK
The solution is model binding: http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/27/disabling-model-binding-on-asp-net-web-apis-beta.aspx