I would like t send data from one of my asp.net application to another. I'm trying to do it using HttpClient.
My First application:
public class PostVacanciesController : Controller
{
public myEntity db = new myEntity();
public const string sendAppURL = "http://localhost:51394/SecondApp/sendData";
public ActionResult PostTest()
{
try
{
PostData dataPost = new PostData();
// Some code to get PostData from database
var myContent = JsonConvert.SerializeObject(dataPost);
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(sendAppURL);
var result = client.PostAsync("", byteContent).Result;
return new HttpStatusCodeResult(HttpStatusCode.OK);
} catch(Exception any)
{
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
}
}
}
My PostData model:
public class PostData
{
public int PropertyID { get; set; }
public List<int> Units { get; set; } = null;
}
Here is my second application here I try to get the data
public class SecondAppController : Controller
{
[EnableCors(origins: "*", headers: "*", methods: "*")]
[System.Web.Http.HttpPost]
public ActionResult sendData(FeedData receiveData)
{
// receiveData variable is wrong.
// It is showing PropertyID = 0 and Units = null
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
}
My FeedData model:
public class FeedData
{
public int PropertyID { get; set; }
public List<int> Units { get; set; } = null;
}
Does anyone know why it is not sending the data? If I create a break point before calling the second App, I can see that it has data to send.
Thanks
List<int>tonull? That's redundant becausenullis the default value for all objects.ByteArrayContentand notStringContent?