繁体   English   中英

ASP.Net Web API URL不起作用

[英]ASP.Net Web api url is not working

我是Web API的新手。
我确定我做错了什么,我的行动没有得到回应。

这是我的行动

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

当我以这种方式调用此操作时http://localhost:38762/api/customer/GetCustomersByCountry/Germany

引发错误,错误消息是

{"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."}

告诉我我在哪里弄错了? 谢谢

Web配置路由为

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

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

编辑:添加完整代码

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);
    }
}

只要告诉我何时控制器将有多个get,其参数名称不同,那我该如何处理这种情况。

谢谢

给定CustomerController之类的

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

}

基于约定的路由可能看起来像这样

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

它将映射http://localhost:38762/api/customer/GetCustomersByCountry/Germany

路由的问题是路由模板中的参数名称不匹配。

另一种选择是使用属性路由

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));
    }    
}

使用此配置

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

        // Other Web API configuration not shown.
    }
}

从网址中的操作中删除“获取”。 只保留CustomersByCountry而不是GetCustomersByCountry。 因此,网址应为http:// localhost:38762 / api / customer / CustomersByCountry / Germany

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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