如何在ASP.NET中添加API端点?

时间:2018-02-05 07:11:59

标签: asp.net-web-api endpoints

我想在ASP.NET中注册API端点,只需在 ApiController 中添加几个方法即可。一种新方法意味着新的API。

在下面的随机示例中:

public class ProductController : ApiController

需要提供以下端点:

/
/price
/price/discount

这里的问题是所有端点都对/有一个GET请求,并产生与/相同的输出。

Reference URLService Contract

1 个答案:

答案 0 :(得分:0)

您可以将Route注释放在要使用自定义路径的方法上。

public class CustomersController : ApiController
{
    // this will be called on GET /Customers or api/Customers can't remember what default
    //config is
    public List<Customer> GetCustomers()
    {
        ...
    }

    // this will be called on GET /My/Route/Customers
    [HttpGet, Route("My/Route/Customers)]
    public List<Customer> GetCustomersFromMyRoute()
    {
        ...
    }

}
相关问题