简体   繁体   中英

Web API Controller is not recognizing newly added methods

I'm adding methods to an additional Web API controller. Testing out one of the existing methods I'm able to hit my break points. Yet if I try to call into one of the new ones I get a 404. I'm testing locally using IIS Express and Postman. Examples below, any idea what could cause this?

When I try to call the new endpoint this is the response I receive:

    {"Message":"No HTTP resource was found that matches the request URI 'http://localhost:53453/api/nisperson/addnewconnection'.",
"MessageDetail":"No action was found on the controller 'NISPerson' that matches the request."}

Existing method:

[HttpPost]
[ActionName("register")]
public ClientResponse PostRegisterPerson(HttpRequestMessage req, PersonModel person)
{
  // This method is getting hit if I call it from Postman
}

endpoint : http://localhost:53453/api/test/register

Newly Added Method:

[HttpPost]
[ActionName("addnewconnection")]
public ClientResponse PostNewConnection(HttpRequestMessage req, string Email, String FirstName, string LastName)
{
  // This is the new method which when called from Postman cannot be found. 
}

Endpoint: http://localhost:53453/api/test/addnewconnection

You defined three required parameters in your method signature ( Email , FirstName and LastName ):

[HttpPost]
[ActionName("addnewconnection")]
public ClientResponse PostNewConnection(HttpRequestMessage req, string Email, String FirstName, string LastName)
{
  // This is the new method which when called from Postman cannot be found. 
}

The route handler cannot map this URI: http://localhost:53453/api/test/addnewconnection to your method because you are not providing those three required parameters.

The correct URI (leaving your method as is) is actually the following one:

http://localhost:53453/api/test/addnewconnection?Email=foo&FirstName=bar&LastName=baz

Either provide those parameters inside the URI as shown or turn them as not required providing a default value:

[HttpPost]
[ActionName("addnewconnection")]
public ClientResponse PostNewConnection(HttpRequestMessage req, string Email = null, String FirstName = null, string LastName = null)
{
  // This is the new method which when called from Postman cannot be found. 
}

Providing a default value will allow you to hit your method using your original URI.

The reason is that it is expecting other parameters (simple string types) to be supplied in querystring which you are not supplying. So, it is trying to call Post with single parameter and failing to find it. Simple types are by default read from URI. If you want them to read from form body, use FromBody attribute.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM