Calling methods in controller class (Asp.net web.api) to work when changing parameter name

Asked

Viewed 814 times

1

I have a question regarding the use of the Asp.net web api with Angularjs that follows below.

I have 2 methods in my controller class (Itenscontroller.Cs):

public void Put(int id, [FromBody]Item value)
{
}

and

[ActionName("UpdateItemFees")]
[HttpPost]
public void UpdateItemFees(int id, [FromBody]Item value)
{         
}

My call on the client is:

$http.put('/api/Itens/Put/' + $scope.osID, item)
$http.post('/api/Itens/UpdateItemFees/' + $scope.osID, item)

This works without problems. The problem is when I change the name of a parameter in these methods, then the system no longer calls the method in Itenscontroller:

public void Put(int osID, [FromBody]Item value)
{
}

and

[ActionName("UpdateItemFees")]
[HttpPost]
public void UpdateItemFees(int osID, [FromBody]Item value)
{         
}

The mistakes are:

No HTTP Resource was found that Matches the request URI

'http://local/api/Itens/Put/3443'.

and

No HTTP Resource was found that Matches the request URI

'http://local/api/Itens/UpdateItemFees/3443'.

Wrong to rename method parameter (from "id" to "osID")?

I have other methods on the page, like GET that are working perfectly, including parameters with custom names (this I find strange).

Thanks in advance.

1 answer

1

Basically, the route to.

Usually, it is configured like this:

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
     name: "DefaultApi",
     routeTemplate: "api/{controller}/{action}/{id}",
     defaults: new { id = System.Web.Http.RouteParameter.Optional }
   );

That is, the route DefaultApi knows a route whose next parameter is id, and the Controller trust that.

If you called the request as follows:

http://local/api/Itens/Put/?osID=3443

Notice that it will work, because then you force the ModelBinder to read a parameter called osID not necessarily defined on the route.

There are a few ways to solve it. One is by setting a route that accepts osID as a parameter without having to put the parameter name in the request address. The other is to use the way I indicated above.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.