简体   繁体   中英

ASP.Net Web api url is not working

I am new in web api .
i am sure i am doing something wrong for which my action is not getting called.

this is my action

public IEnumerable<Customer> GetCustomersByCountry(string country)
{
    return repository.GetAll().Where(
        c => string.Equals(c.Country, country, StringComparison.OrdinalIgnoreCase));
}

when i am calling this action this way http://localhost:38762/api/customer/GetCustomersByCountry/Germany

the error is thrown, and error message is

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:38762/api/customer/GetCustomersByCountry/Germany'.","MessageDetail":"No action was found on the controller 'Customer' that matches the request."}

tell me where i made the mistake ? thanks

Web config routes are

    config.Routes.MapHttpRoute(
        name: "WithActionApi",
        routeTemplate: "api/{controller}/{action}/{customerID}"
    );

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

EDIT : Full code added

public class CustomerController : ApiController
{
    static readonly ICustomerRepository repository = new CustomerRepository();

    public IEnumerable<Customer> GetAllCustomers()
    {
        return repository.GetAll();
    }

    public Customer GetCustomer(string customerID)
    {
        Customer customer = repository.Get(customerID);
        if (customer == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        return customer;
    }

    //[ActionName("GetCustomersByCountry")]
    public IEnumerable<Customer> GetCustomersByCountry(string country)
    {
        return repository.GetAll().Where(
            c => string.Equals(c.Country, country, StringComparison.OrdinalIgnoreCase));
    }

    public HttpResponseMessage PostCustomer(Customer customer)
    {
        customer = repository.Add(customer);
        var response = Request.CreateResponse<Customer>(HttpStatusCode.Created, customer);

        string uri = Url.Link("DefaultApi", new { customerID = customer.CustomerID });
        response.Headers.Location = new Uri(uri);
        return response;
    }

    public void PutProduct(string customerID, Customer customer)
    {
        customer.CustomerID = customerID;
        if (!repository.Update(customer))
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
    }

    public void DeleteProduct(string customerID)
    {
        Customer customer = repository.Get(customerID);
        if (customer == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        repository.Remove(customerID);
    }
}

just tell me when controller will have multiple get whose parameter name is different then how could i handle the situation.

thanks

Given CustomerController like

public class CustomerController : ApiController {
    [HttpGet]
    public IEnumerable<Customer> GetCustomersByCountry(string country) {
        return repository.GetAll().Where(
            c => string.Equals(c.Country, country, StringComparison.OrdinalIgnoreCase));
    }

}

a convention-based route can look like this

config.Routes.MapHttpRoute(
    name: "CustomerApi",
    routeTemplate: "api/customer/{action}/{countryId}",
    default: new { controller = "Customer"}
);

which will map http://localhost:38762/api/customer/GetCustomersByCountry/Germany

The problem with your route is that your parameter name in the route template does not match.

Another option could be to use attribute routing

Attribute Routing in ASP.NET Web API 2

[RoutePrefix("api/customer")]
public class CustomerController : ApiController {
    //GET api/customer/country/germany
    [HttpGet, Route("country/{country}")]
    public IEnumerable<Customer> GetCustomersByCountry(string country) {
        return repository.GetAll().Where(
            c => string.Equals(c.Country, country, StringComparison.OrdinalIgnoreCase));
    }    
}

with this configuration

public static class WebApiConfig {
    public static void Register(HttpConfiguration config) {
        // Web API routes
        config.MapHttpAttributeRoutes();

        // Other Web API configuration not shown.
    }
}

Remove "Get" from the action in the url. Just keep CustomersByCountry instead of GetCustomersByCountry. So the url should be http://localhost:38762/api/customer/CustomersByCountry/Germany .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM