-1

I am new to the WebAPI and I got stuck in a problem. I am using API controller which contains two Get methods. One is used for GetAll while other retrieves data on the basis of ID. Now what I want to do is to implement another get method which takes string and returns records. I have made that method and call that it does not work as my route was default API route which was like:

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

When I added a new route to call my custom GetMethod:

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

I was able to call my custom GetMethod but now I lose the restful method with id parameters. How can I use both methods?

Opal
  • 81,889
  • 28
  • 189
  • 210
  • I have also try using Attribute routing like [Route(api/controllername/action)] on the top of the method but it still did not works. – Muhammad Awais May 16 '15 at 16:45

2 Answers2

0

Have you tried using your second route only and call as

api/customer/get/1
api/customer/getall/
api/customer/getmethod/one

In the above, customer is the controller name. You have to replace with yours.

Please check whether you have the routes in the webapiconfig.cs file. Please refer this article for more help.

wonderful world
  • 10,969
  • 20
  • 97
  • 194
  • I have tried this i was able to call api/customer/get/1 and api/customer/getall but for api/customer/getmethod/one i am getting error "{"Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'CustomersProject.Models.BaseModel Get(Int32)' in 'CustomersProject.Controllers.BaseController`2[CustomersProject.Models.Customer,CustomersProject.ApiModel.CustomerApiModel]'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."}" – Muhammad Awais May 16 '15 at 19:15
  • You may need to have one more route similar to the second one, but have a constraint to say id is of type 'string'. If both the methods do the same thing, a better design would be to have only one GET and the client convert the string id to int id and call the single get method. – wonderful world May 16 '15 at 19:59
  • The problem is that when i added a route to the web.config i have to place that route above the default route Then i lost the restful services i want the id method to be called as api/customer/1 ,the getall method to be api/customer and my method for search as customer/get/kashif where kashif is the name to search for. – Muhammad Awais May 17 '15 at 03:55
0

Better not to change routes. You can use Action Name to differentiate the calls and can add more functions. Take a look at this answer: How to add custom methods to ASP.NET WebAPI controller?

Community
  • 1
  • 1
  • i had changed my routes to defaults and make my methods look like this... [ActionName("search")] [HttpGet] public virtual BaseModel Get(string name) { return new BaseModel() { Success = true }; } and call like this api/customer/search but it still not works – Muhammad Awais May 16 '15 at 19:34