Daina Hodges
Daina Hodges

Reputation: 853

Pass Web API with different parameters to pass them to stored procedures

I have a Web API with about 6 stored procedures, each stored procedures has different parameter, for example 2 params or 3 params or 1 param and one stored procedures takes 0 param.
How can I pass those params to my web api?

This is my webConfig.cs

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 = RouteParameter.Optional }
    );

    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}

And my Controllers code:

public class channelsController : ApiController
{
    [HttpGet]
    [Route("api/channels/{code:string}/{search_by:string}")]
    public IEnumerable<sp_get_channels_Result> Get(string code,string search)
    {
        myEntities db = new myEntities();
        return db.sp_get_channels("", "all").AsEnumerable();
    }

}
//no param    
public class networksController : ApiController
{             
    [HttpGet]
    public IEnumerable<sp_get_networks_Result> Get()
    {
        myEntities db = new myEntities();
        return db.sp_get_networks().AsEnumerable();
    }
}

But I get this error:
The inline constraint resolver of type DefaultInlineConstraintResolver was unable to resolve the following inline constraint: string.

Upvotes: 0

Views: 1084

Answers (2)

hasan
hasan

Reputation: 3502

can you try to change "search_by" into "search"

[HttpGet]
[Route("api/channels/{code}/{search}")]
public IEnumerable<sp_get_channels_Result> Get(string code,string search)
{
   myEntities db = new myEntities();
   return db.sp_get_channels("", "all").AsEnumerable();
}

Upvotes: 1

Craig H
Craig H

Reputation: 2071

:string

Is not a valid route constraint. See the list of default constraints here: https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-constraints

You could write your own, but it's not there by default.

Upvotes: 1

Related Questions