我正在使用.NET 4.5和Web API 2的最终发行版本(在Visual Studio 2013中)。我一直在使用this documentation作为参考,但无济于事。
我有一些基本路线,如
api/providers
api/locations
api/specialties
每个方法都有一些方法
Get()
Get(int id)
Get(string keyword)
Autocomplete(string keyword)
Search(string zipcode, string name, int radius, [...])
理想情况下,我希望网址最终会像
一样在下面的代码块中,Get
方法和Search
按预期工作,但Autocomplete
没有。应该注意的是,我在多个控制器中有类似的命名方法。我究竟做错了什么? (另外,Name =
属性究竟是什么?)
/// <summary>
/// This is the API used to interact with location information.
/// </summary>
[RoutePrefix("api/locations")]
public class LocationController : ApiController
{
private ProviderEntities db = new ProviderEntities();
private static readonly Expression<Func<Location, LocationAutocompleteDto>> AsLocationAutocompleteDto =
x => new LocationAutocompleteDto
{
Id = x.Id,
Name = x.Name
};
/// <summary>
/// Get ALL locations.
/// </summary>
[Route("")]
public IQueryable<Location> Get()
{
return db.Locations.AsQueryable();
}
/// <summary>
/// Get a specific location.
/// </summary>
/// <param name="id">The ID of a particular location.</param>
[Route("{id:int}")]
public IQueryable<Location> Get(int id)
{
return db.Locations.Where(l => l.Id == id);
}
/// <summary>
/// Get all locations that contain a keyword.
/// </summary>
/// <param name="keyword">The keyword to search for in a location name.</param>
[Route("{keyword:alpha}")]
public IQueryable<Location> Get(string keyword)
{
return db.Locations.Where(l => l.Name.Contains(keyword)).OrderBy(l => l.Name);
}
[Route("search", Name = "locationsearch")]
public string Search(string zipcode = null, string latitude = null, string longitude = null)
{
if (zipcode != null) return "zipcode";
if (latitude != null && longitude != null) return "lat/long";
else
return "invalid search";
}
/// <summary>
/// Autocomplete service for locations, returns simple Id/Name pairs.
/// </summary>
/// <param name="keyword">The keyword to search on.</param>
[Route("autocomplete/{keyword:alpha}", Name = "locationautocomplete")]
public IQueryable<LocationAutocompleteDto> Autocomplete(string keyword)
{
// validate the inputs
if (string.IsNullOrEmpty(keyword))
return null;
IQueryable<Location> results = from l in db.Locations
where l.Name.Contains(keyword)
select l;
return results.Select(AsLocationAutocompleteDto);
}
对于这些内容的最新版本,似乎没有很多社区支持。
简单地替换
[Route("autocomplete/{keyword:alpha}", Name = "locationautocomplete")]
与
[Route("autocomplete"), Route("autocomplete/{keyword:alpha}", HttpGet()]
它完美无缺。
答案 0 :(得分:5)
您是否尝试对搜索和自动完成操作执行GET请求?如果是,那么您需要使用System.Web.Http.HttpGet
属性进行装饰。如果某个操作没有像Get,Put,Post,Delete等动词作为前缀,则默认情况下Web API会假定为POST。我猜你收到了405方法不允许......正确吗?
在链接生成(Url.Link
)的情况下,Name属性非常有用,您需要指定名称