api - 多个 get 方法的 API 路由
问题描述
我的 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,
{
...
}
如何为这些配置路由?我在使用默认路由调用它们时遇到问题。
谢谢, 马汉特什
解决方案
我假设您使用的是 Web Api 2。您可以使用Route 属性:
为了使 http 属性路由可用,您必须在中添加以下行App_Start\WebApiConfig.cs
:
static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
}
}
然后将路由属性添加到方法中。
[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 Not Found。
在 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)
{
...
}
推荐阅读
- ruby-on-rails - 数组上的Vue js trucate属性
- javascript - 为什么我的 javascript dispatchEvent 不起作用
- sql - Visual Studio SSRS 日历报表详细信息未分组
- reactjs - 更改包名称 React Native
- wso2 - WSO2 Micro Gateway 安装和架构
- optimization - 在 GPflow 2.0 中使用 Tensorflow 双射器链进行边界超参数优化
- javascript - 使用javascript在多个相同的html元素中添加一个段落
- django - 一种在不删除 Django 中早期数据的情况下更新数据库中数据的方法
- jenkins - 有没有办法跟踪 Jenkins 中全局共享库的使用情况?
- r - 删除 stat_density_2d(geom = 'point') 中密度为 0(无数据)的点