I have two websites ABC and XYZ.I am trying to send a xml data from ABC to XYZ.
To be specific with the functionality: I am using the ABC application after logging in with EmailID. When I click on a button in the ABC application , the site should redirect to XYZ site along with the credentials. I am sending the data to XYZ in XML format via headers.
public async Task<object> Login()
{
string url = "http://localhost:49669/**api**/Account/PostData";
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", "Basic Auth");
string xmlString = "";
UserDetails userDetail = new UserDetails { UserID = 1, EmailID = "name@redblacktree.com" };
XmlDocument xmlDoc = new XmlDocument(); //Represents an XML document,
//Initializes a new instance of the XmlDocument class.
XmlSerializer xmlSerializer = new XmlSerializer(userDetail.GetType());
var emptyNs = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
// Creates a stream whose backing store is memory.
using (MemoryStream xmlStream = new MemoryStream())
{
xmlSerializer.Serialize(xmlStream, userDetail, emptyNs);
xmlStream.Position = 0;
//Loads the XML document from the specified string.
xmlDoc.Load(xmlStream);
xmlString = xmlDoc.InnerXml;
}
var stringContent = new StringContent(xmlString.ToString(), Encoding.UTF8, "application/xml");
httpClient.DefaultRequestHeaders.Add("UserDetails", xmlString.ToString());
var response = httpClient.PostAsync(url, stringContent).Result;
var result = response.Content.ReadAsStringAsync().Result;
if (response.StatusCode == HttpStatusCode.OK)
{
var messageContents = await response.Content.ReadAsStringAsync();
return Content(messageContents);
}
return null;
}
Here UserDetails is a model class.
public class AccountController : ApiController
{
[System.Web.Http.HttpPost]
public HttpResponseMessage PostData(HttpRequestMessage request)
{
var someText = request.Content.ReadAsStringAsync().Result;
var header1 = request.Headers.Authorization;
var header2 = request.Headers.GetValues("UserDetails");
return new HttpResponseMessage();
}
If I am posting to a API Controller, it works. If it is to a normal MVC controller, it is not working. Can anyone tell me why.
I am trying to post to a MVC Controller because, I am finding it difficult to get the POST data from the API Controller to MVC Controller. So if I can directly post to MVC Controller, it eases my work. Any help ?
Solved!
Simply by providing return type as ActionResult solved my problem. Unnecessarily I complicated the code.
public class AccountController : Controller
{
[System.Web.Http.HttpPost]
public ActionResult PostData()
{
var data = HttpContext.Request.Headers["UserDetails"];
return null;
}
}
Thanks !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.