Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48197

Create multiple Web Api with same signature

I have a CAR controller and create my API to Get a list of cars with detailed information of each car in a GRID and work OK.

Now Im creating a ListTree for the cars and only need the basic details. ID and Color

How I create another method GetSimpleCar() with same signature in current controller or should I create another controller CarSimple

Current I have:

public List<dtoCar> Get()
{
   List<dtoCar> result = db.cars.Select(
                                 r => new dtoCar
                                 {
                                   Car_ID = r.car_id,
                                   X = r.x,
                                   Y = r.y,
                                   RoadName = r.rto.name,
                                   Azimuth = (int)r.avl.azimuth,
                                   Color = r.Color,
                                   DateTime = r.datetime.Value, 
                                   Geom = r.geomtext
                                 }).ToList();
   return result;
}

I dont want use the current API to create the Tree.

$('#ajax').jstree({
    'core': {
        'data': {
         -- RIGHT NOW
            "url": "http://localhost/TreeTest/api/Cars/"
                                                  ^^   
                                                 Controller
         -- DESIRE CHANGE
            "url": "http://localhost/TreeTest/api/Cars/GetSimpleCar"
                                                            ^^ 
                                                       Second Method?
        } 
    },
    "checkbox": {
        "keep_selected_style": false
    },
    "plugins": ["wholerow", "checkbox"]
});

Upvotes: 1

Views: 2503

Answers (1)

Chris
Chris

Reputation: 2019

You can set a fixed route in the same controller like this:

using System.Web.Http;
...    
[HttpGet]
[Route("api/Cars/GetSimpleCar")]
public object GetCarsButOnlyIdAndColor()
{
    return db.cars.Select(r => new
    {
        Car_ID = r.car_id,
        Color = r.Color,
    }).ToList();
}

WebApi doesn't care about the method's name, it's the route that matters.

And like vendettamit said, you will need to Enable Attribute Routing

Documentation:

Routing and Action Selection in ASP.NET Web API

Attribute Routing in ASP.NET Web API 2

Upvotes: 3

Related Questions