XML : how to pass object to web api and get xml response as result of post

I have the following class

  public class Product       {           public int Id { get; set; }           public string Name { get; set; }           public string Category { get; set; }           public decimal Price { get; set; }       }   And trying to pass this product class through httprequest as follows :   Product prod = new Product              {                  ID = 1,                  Name = "Pen",                  Category= "Stationery"              };                string url = "http://localhost:51892/Home/index";                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);              WebProxy proxy = new WebProxy();              proxy.BypassProxyOnLocal = true;              request.Proxy = proxy;              request.ContentType = "application/xml; encoding=utf-8";              request.Method = "POST";              DataContractSerializer ser = new DataContractSerializer(prod.GetType());              Stream requestStream = request.GetRequestStream();              requestStream.Close();                  HttpWebResponse response;              response = (HttpWebResponse)request.GetResponse();              if (response.StatusCode == HttpStatusCode.OK)              {                  Stream responseStream = response.GetResponseStream();                  string responseStr = new StreamReader(responseStream).ReadToEnd();                }    

I need to post this object as xml in the controller. And My controller is code is :

  public class HomeController : Controller      {          public ActionResult Index()          {              return View();          }            [HttpPost]          public string ReturnXmlDocument(HttpRequestMessage request)          {              var doc = new XmlDocument();              doc.Load(request.Content.ReadAsStreamAsync().Result);              return doc.DocumentElement.OuterXml;          }      }    

Can anyone please help me how to get the xml since its not working? As part of testing, just created console application and calling the url

No comments:

Post a Comment