0
votes

Here is my Web Config

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services

    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = System.Web.Http.RouteParameter.Optional }
    );
    config.Formatters.JsonFormatter.SupportedMediaTypes
        .Add(new MediaTypeHeaderValue("text/html"));
}

Here the controller

// GET api/<controller>  
public IEnumerable<SPS_ONHAND> Get()
{
    //returning all records of table tblMember.  
    return db.SPS_ONHANDs.ToList().AsEnumerable();
}


// GET api/<controller>/name/ITEM_CODE  
[Route("api/SPSOnHand/{item_code}")]
[HttpGet]
public IEnumerable<SPS_ONHAND> Get(string item_code)
{
    var list = from g in db.SPS_ONHANDs 
               where g.ITEM_CODE == item_code 
               select g;

    return list;
}

// This action method will fetch and filter
// for specific member id record  

// GET api/<controller>/5
[Route("api/SPSOnHand/{id}")]
[HttpGet]
public HttpResponseMessage Get(int id)
{
    // fetching and filter specific member id record   
    var memberdetail = (from a in db.SPS_ONHANDs 
                        where a.ID == id 
                        select a).FirstOrDefault();

    // checking fetched or not with the help of NULL or NOT.  
    if (memberdetail != null)
    {
        // sending response as status code
        // OK with memberdetail entity.  
        return Request.CreateResponse(
            HttpStatusCode.OK, 
            memberdetail);
    }
    else
    {
        // sending response as error status code
        // NOT FOUND with meaningful message.  
        return Request.CreateErrorResponse(
            HttpStatusCode.NotFound, 
            "Invalid Code or Member Not Found");
    }
}

here is the error when I try to use get request

{ "Message": "An error has occurred.", "ExceptionMessage": "Multiple actions were found that match the request: \r\nGet on type SPInventory.Controllers.SPSOnHandController\r\nGet on type SPInventory.Controllers.SPSOnHandController", "ExceptionType": "System.InvalidOperationException", "StackTrace": " at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()" }

Thank you in advance. I'm still newbie with web api

1
Controllers cannot differentiate routes based on input type, so asp.net is unsure where to redirect your request. You could explicitly add [Route("api/SPSOnHand/{id:int}")] and this should differentiate integer and the string and route properly. A better approach would be to have different routes.Sharath N S

1 Answers

0
votes

Controller doesn't know what the name of your route value - is it item_code or id. I recommend you to use this route attributes:

Route("~/api/SPSOnHandByItemCode/{item_code}")]
public IEnumerable<SPS_ONHAND> GetByItemCode(string item_code)

and

[Route("~/api/SPSOnHandById/{id}")]
public HttpResponseMessage GetById(int id)