0
votes

I am trying to create a WebAPI controller with multiple Get commands using the ActionName method. I successfully did this on another project, but have been having problems with this latest project and cannot see to understand why my knockout view model ajax call cannot find the specific URI.

WebApiConfig.cs:

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

Controller:

    // GET api/lot
    [ActionName("Default")]
    public IEnumerable<DataObject> Get()
    {
         //...
    }

    // GET api/lot/Specific/5
    [ActionName("Specific")]
    public IEnumerable<DataObject> Get(int? data)
    {
        //...
    }

    // GET api/lot/5
    public string Get(int id)
    {
        return "value";
    }

My default action for GET works perfect but the Specific action continues to have this error when I attempt to call it from the view-model:

"Failed to load resource: the server responded with a status of 404 (Not Found)"

I added [HttpGet] next to the [ActionName("Specific")] and had the following error:

"GET http://localhost:57492/api/lot/Specific/1 404 (Not Found)"

I've tried several different things such as removing the int? data argument, but then when I attempt to build the project it tells me an existing function with same arguments already exists, even with different action names.

Ultimately I would like to have multiple Get(parameter) actions to call for interacting with my view-model.

2

2 Answers

1
votes

I was able to solve this by adding the Route property with HttpGet and renaming all of my functions to unique names like GetAll(), GetSpecific(ind id), etc..

    // GET api/lot
    [HttpGet]
    [Route("api/lot/GetAll")]
    public IEnumerable<DataObject> GetAll()
    {...}

    // GET api/lot/GetSpecific/{id}
    [HttpGet]
    [Route("api/lot/GetSpecific/{id}")]
    public IEnumerable<DataObject> GetSpecific(string id)
    {
0
votes

Adding a new route in config.Routes as below. Will this help? I didn't test this.

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