Custom Method Names in ASP.NET Web API

Custom method names in ASP.NET Web API

By default the route configuration follows RESTFul conventions meaning that it will accept only the Get, Post, Put and Delete action names (look at the route in global.asax => by default it doesn't allow you to specify any action name => it uses the HTTP verb to dispatch). So when you send a GET request to /api/users/authenticate you are basically calling the Get(int id) action and passing id=authenticate which obviously crashes because your Get action expects an integer.

If you want to have different action names than the standard ones you could modify your route definition in global.asax:

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

Now you can navigate to /api/users/getauthenticate to authenticate the user.

ASP.Net MVC 4 API custom method name

If your routes are configured to be these (default in the MVC solution template):

url: "{controller}/{action}/{id}"

You should change parcelLabelNumber to id.

You can read more about routes here.

How to add custom methods to ASP.NET WebAPI controller?

You can use attributes such as the RoutePrefix with the Http type.

[Route("ChangePassword")]
[HttpPost] // There are HttpGet, HttpPost, HttpPut, HttpDelete.
public async Task<IHttpActionResult> ChangePassword(ChangePasswordModel model)
{
}

The http type will map it back to its correct method in combination with the Route name.

unable to call web api using custom methods

If you are using web api 2, then you can use Attribute Routing
Follow this url, I hope you will get your answer

Sample:

[Route("customers/{customerId}/orders")]
public IEnumerable<Order> GetOrdersByCustomer(int customerId) { ... }

To know more detail follow this link
http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

There is another way you can use custom methode by writing your own route in WebApiConfig.cs
you can found this detail in this link
http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

Hope this will help you.

Calling custom methods in API controller

The familyId is not mapped in the route. So you can create a new Mapping wich adds familyId to the id mapping or rewrite the parameter of GetPeopleInFamily to id.

Rewrite:

public string GetPeopleInFamily(string id)

Create new route: When you create a new route be sure that you remove the mapping with the id otherwise it won't work.

routes.MapHttpRoute("DefaultApiWithActionAndFamilyId", "Api/{controller}/{action}/{familyId}", new { familyId = RouteParameter.Optional }, new { familyId = @"\d+" });

Consume Web API haing Action methods with custom name

you need to configure your route to include the action like api/{controller}/{action}/{id} and make call like api/customer/CreateCustomer

Sample Image

Sample Image

from C#,

var t = new HttpClient().GetAsync("http://localhost:63154/api/UserApi/CreateCustomer").Result.Content.ReadAsStringAsync().Result;

ASP.NET Web API 2 custom routing to specific method

In your Startup.cs register attribute routes:

config.MapHttpAttributeRoutes();

And in the controller:

[RoutePrefix("healthcheck")]
public class HealthCheckController : ApiController
{
[HttpGet]
[Route]
public IHttpActionResult GetHealthCheckStatus()
{
return Ok();
}
}


Related Topics



Leave a reply



Submit