1

There is a node.js server and I'm going to call my web api controller when a client connected with node.js server using a websocket. Im' using socket.io to connect client with node.js server, then node.js server sends a xhr POST request to web api controller. but getting the response as, "{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:4928/api/Player'.","MessageDetail":"No action was found on the controller 'Player' that matches the request."}"

Web API Method

    public class PlayerController : ApiController
{
      public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    public string Post(string value)
    {
        return value;
    }
}

Node.js

 var app = require('express')();
var http = require('http').Server(app);
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
var querystring = require('querystring');
var io = require('socket.io')(http);
io.on('connection', function (socket) {
    console.log('A user connected ' + socket.handshake.address);
    PostRequest(socket.handshake.address);
    //Whenever someone disconnects this piece of code executed
    socket.on('disconnect', function () {
        console.log('A user disconnected ' + socket.handshake.address);
    });
});


function PostRequest(data) {

    var url = "http://localhost:4928/api/Player";
    var params = "value=" + data;/* querystring.stringify({ "value": data })*/
    xhr.open('POST', url, true);
    xhr.onreadystatechange = function () {//Call a function when the state changes.
        if (xhr.readyState == 4 && xhr.status == 200) {
            console.log(xhr.responseText);
        }
        console.log(xhr.responseText);

    }
    xhr.send(querystring.stringify({ value: "sss" }));

}


http.listen(3000, function () {
    console.log('listening on *:3000');
});

Anyway this is working when I'm call the get method sending a xhr get request

2
  • Try putting [HttpPost] attribute on your Post action method. Commented Jun 16, 2018 at 20:20
  • @MohsinMehmood The result is same. But when I remove parameters from web api post method and call without passing parameters it's working. Please help Commented Jun 17, 2018 at 2:19

1 Answer 1

0

You need to inform Web API to look for the value parameter in the Request body since you're not sending data via query string. Try modifying your Post action signature:

public string Post([FromBody]string value)
{
    ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

If he is passing the value in query parameters then [FromUrl] attribute will be there!
@OneManBand Now the Post method is calling after adding [FromBody] attribute. But the Value parameter is getting null.
Hmm. Try using JSON.stringify() instead of querystring.stringify().

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.