多种获取方法的api路由

时间:2018-08-16 03:57:03

标签: api get routes

我的api中有两个Get方法,如下所示:

public IHttpActionResult GetCandidateProfilesByProfileID(long id)
{
......
}

public IHttpActionResult GetCandidatesBySearchCrietria( string FName= null,string LastName = null, Nullable<DateTime> DoB = null, string City = null,string zipCode = null, string stateID = null,string education = null,
{
...
}

如何为这些配置路由?我在使用默认路由调用它们时遇到了问题。

谢谢, Mahantesh

1 个答案:

答案 0 :(得分:0)

我假设您正在使用Web Api2。您可以使用Route attribute

为了使http属性路由可用,您必须在App_Start\WebApiConfig.cs中添加以下行:

static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
    }
}

然后将route属性添加到方法中。

[HttpGet]
[Route("GetCandidateProfilesByProfileID")]
public IHttpActionResult GetCandidateProfilesByProfileID(long id)
{
    ......
}

[HttpGet]
[Route("GetCandidatesBySearchCrietria")]
public IHttpActionResult GetCandidatesBySearchCrietria( string FName= null,string LastName = null, Nullable<DateTime> DoB = null, string City = null,string zipCode = null, string stateID = null,string education = null)
{
    ...
}

请注意,某些参数是必需的。如果省略,将导致找不到Http 404

在asp.net Core中,您可以使用 HttpGet 属性:

[HttpGet("GetCandidateProfilesByProfileID")]
public IHttpActionResult GetCandidateProfilesByProfileID(long id)
{
    ......
}

[HttpGet("GetCandidatesBySearchCrietria")]
public IHttpActionResult GetCandidatesBySearchCrietria( string FName= null,string LastName = null, Nullable<DateTime> DoB = null, string City = null,string zipCode = null, string stateID = null,string education = null)
{
    ...
}