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
3
  • Just checking, why haven't you declared your cmd parameter with the FromBody attribute like this: public HttpResponseMessage Post([FromBody] Command cmd)? Commented Sep 6, 2017 at 7:35
  • Possible duplicate of stackoverflow.com/questions/45862459/…. Commented Sep 6, 2017 at 7:43
  • 1
    I debugged a bit further, and found that the problem was caused by having the contentType: 'application/json' in the ajax call.. Commented Sep 6, 2017 at 7:52

2 Answers 2

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(";-)");
    ...
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.