2

Using Northwind database I can search through the tables via an Int id which is fine but on the customer table, the Customer ID is a string, my api controller won't run if I try and search for a customer entering a specific string I can return all but not an individual how can I tell the controller to search for a string id ie Joe Bloggs CustId=NVAMD ?

[RoutePrefix("api/cust")]

 [RoutePrefix("api/cust")]
public class CustomersController : ApiController
{

    private NORTHWNDEntities db = new NORTHWNDEntities();
    [Route("getall")]
    // GET: api/Customers
    public IQueryable<Customer> GetCustomers()
    {
        return db.Customers;
    }

    // GET: api/Customers/5
    [ResponseType(typeof(Customer))]
    [Route("getcustid/{id:string}")]
    public dynamic GetCustomer(string id)
    {
        Customer customer = db.Customers.Find(id);
        if (customer == null)
        {
            return NotFound();
        }

        return Ok(customer);
    }

    [Route("getcustadd/{id:string}")]
    public dynamic GetCust(string id)
    {

        Customer cust = db.Customers.Find(id);
        if (cust == null)
            return NotFound();
        return Ok(cust);

    }
1
  • here is the error being returned on compile {"The inline constraint resolver of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: 'string'."}
    – user2915637
    Mar 24, 2015 at 12:19

1 Answer 1

3

You must remove the "string" constraint, because it's not supported by Web API 2

// GET: api/Customers/5
    [ResponseType(typeof(Customer))]
    [Route("getcustid/{id}")]
    public dynamic GetCustomer(string id)
    {
        Customer customer = db.Customers.Find(id);
        if (customer == null)
        {
            return NotFound();
        }

        return Ok(customer);
    }

You can read more on the following link: http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#constraints

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy