최종 릴리스 버전의 .NET 4.5 및 웹 API 2 (Visual Studio 2013)를 사용하고 있습니다. 나는 this documentation을 참고로 사용하고 있습니다. 웹 API 2 라우팅 특성이 작동하지 않습니다.
는 내가처럼 결국 URL을하고 싶습니다,api/providers
api/locations
api/specialties
같은 몇 가지 기본 노선과 이상적으로
Get()
Get(int id)
Get(string keyword)
Autocomplete(string keyword)
Search(string zipcode, string name, int radius, [...])
같은 각에 대한 몇 가지 방법이
- /api/locations/12345 (위치 12345 가져 오기)
- /API/위치/임상 (이름에 "임상"로 위치를 얻을)
- /API/위치/자동 완성? 키워드 = 임상 ("임상와 위치에 대한 압축 된 아이디 + 이름 개체를 가져 ") 이름으로
- /API/위치/검색? 우편 번호 = 12345 & 반경 = 20 & 이름 = 임상 (수와 우편 번호 12345의 20 마일 이내에 위치") 이름으로 "임상
아래 코드에서 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"), Route("autocomplete/{keyword:alpha}", HttpGet()]
와
[Route("autocomplete/{keyword:alpha}", Name = "locationautocomplete")]
를 교체하고 완벽하게 작동합니다.
감사합니다 이름을 지정해야 할 곳! Route ("autocomplete/{keyword : alpha}"), Name = "locationautocomplete")]'Route ("autocomplete"), Route ("자동 완성/{keyword : 알파}"), HttpGet()]'이제 완벽하게 작동합니다. 'HttpGet()'속성이 핵심이었고 두 개의'Route()'정의를 함께 묶어서 내가 원하는 두 가지 방식으로 작동하게했습니다. –