0

I am struggling with getting my self hosted asp.net web api server to get a call to post on a controller.

$.ajax({
    url: mySelfHostedAspWebApi, //i.e. http://1.2.3.4:1234/
    type: "POST",
    data: cmd,
    contentType: 'application/json',
    success: (function(scanCmd) {

   }), //success: (function (scanCmd){
});// $.ajax({

And the asp.net core controller in C# is pretty straight.

  ...
  [Route( "" )]
  [HttpPost]
  public HttpResponseMessage Post(Command cmd)
  {
     ASSERT(null,cmd); // <=== problem here !!!
     if(cmd=="go") Go(";-)");
     ...

In one of my old projects, I came across a similar kind of problem... And the fix was to open some file on the system, that controlled if the post was availible for myselfhostedapp and write the POST on the line that controlled the access to my application. (I just cannot recall what file it was...)

  • if this sounds familiar, please add a hint.. thanks
kfn
  • 620
  • 1
  • 7
  • 26
  • Just checking, why haven't you declared your `cmd` parameter with the `FromBody` attribute like this: `public HttpResponseMessage Post([FromBody] Command cmd)`? – Quality Catalyst Sep 06 '17 at 07:35
  • Possible duplicate of https://stackoverflow.com/questions/45862459/asp-net-core-2-api-post-objects-are-null/45862556. – Kirk Larkin Sep 06 '17 at 07:43
  • 1
    I debugged a bit further, and found that the problem was caused by having the contentType: 'application/json' in the ajax call.. – kfn Sep 06 '17 at 07:52

2 Answers2

1

I reckon you are missing the FromBody parameter declaration for your cmd parameter like this:

[Route( "" )]
[HttpPost]
public HttpResponseMessage Post([FromBody] Command cmd) // <<- tweak here
{
    ASSERT(null,cmd); // <=== no problem here :-)
    if(cmd=="go") Go(";-)");
    ...
Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
0

the problem was in the use of contentType: 'application/json' in the ajax call. - I removed that, and got the data across.

kfn
  • 620
  • 1
  • 7
  • 26