라우팅 및 컨트롤러에 대한 많은 질문을 읽었지 만 찾고있는 것을 찾을 수 없습니다. 나는이 구조를 가진이 컨트롤러를 가지고있다 :요청과 일치하는 여러 작업이 발견되었습니다.
업데이트 : 포함 된 전체 클래스 소스.
public class LocationsController : ApiController
{
private readonly IUnitOfWork _unitOfWork;
public LocationsController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
// GET /api/locations/id
public Location Get(Guid id)
{
return this.QueryById<Location>(id, _unitOfWork);
}
// GET /api/locations
public IQueryable<Location> Get()
{
return this.Query<Location>(_unitOfWork);
}
// POST /api/locations
public HttpResponseMessage Post(Location location)
{
var id = _unitOfWork.CurrentSession.Save(location);
_unitOfWork.Commit();
var response = Request.CreateResponse<Location>(HttpStatusCode.Created, location);
response.Headers.Location = new Uri(Request.RequestUri, Url.Route(null, new { id }));
return response;
}
// PUT /api/locations
public Location Put(Location location)
{
var existingLocation = _unitOfWork.CurrentSession.Query<Location>().SingleOrDefault(x => x.Id == location.Id);
//check to ensure update can occur
if (existingLocation == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
//merge detached entity into session
_unitOfWork.CurrentSession.Merge(location);
_unitOfWork.Commit();
return location;
}
// DELETE /api/locations/5
public HttpResponseMessage Delete(Guid id)
{
var existingLocation = _unitOfWork.CurrentSession.Query<Location>().SingleOrDefault(x => x.Id == id);
//check to ensure delete can occur
if (existingLocation != null)
{
_unitOfWork.CurrentSession.Delete(existingLocation);
_unitOfWork.Commit();
}
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
// rpc/locations
public HttpResponseMessage Dummy()
{
// I use it to generate some random data to fill the database in a easy fashion
Location location = new Location();
location.Latitude = RandomData.Number.GetRandomDouble(-90, 90);
location.Longitude = RandomData.Number.GetRandomDouble(-180, 180);
location.Name = RandomData.LoremIpsum.GetSentence(4, false);
var id = _unitOfWork.CurrentSession.Save(location);
_unitOfWork.Commit();
var response = Request.CreateResponse<Location>(HttpStatusCode.Created, location);
response.Headers.Location = new Uri(Request.RequestUri, Url.Route(null, new { id }));
return response;
}
}
그리고 내 경로 정의 (Global.asax에) : 지금까지
public static void RegisterRoutes(RouteCollection routes)
{
// Default route
routes.MapHttpRoute(
name: "Default",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// A route that enables RPC requests
routes.MapHttpRoute(
name: "RpcApi",
routeTemplate: "rpc/{controller}/{action}",
defaults: new { action = "Get" }
);
}
내가 가진 브라우저 치면 :
가[baseaddress]/locations/s0m3-gu1d-g0e5-hee5eeeee
// 그것은[baseaddress]/locations/
작동을 // 복수 검색 결과
[baseaddress]/rpc/locations/dummy
// 그것은 이상한 일이 일부 업데이트를 수행하는 동안 내 NuGet으로 엉망이 될 때까지이,를 작동하는 데 사용 이다
작동합니다. 내가 여기서 무엇을 놓치고 있니?
GET, POST, PUT 또는 delete로 시작하는 동사가 첫 번째 경로에 자동 매핑되고 rpc를 통해 제 2 경로에 속하는 더미 테스트 메서드가 호출됩니다. MyProject를 입력에 System.Linq.IQueryable`1가 [Myproject.Domain.Location] 받기() : 슬로우됩니다
오류가
여러 작업 요청을 일치 발견 된 메시지
InvalidOperationException
입니다. 형 Myproject.Webservices.Controllers.LocationsController
어떤 아이디어가 Webservices.Controllers.LocationsController System.Net.Http.HttpResponseMessage 더미()?
Trimming 된 RPC 메소드를 포함하여 LocationsController에 대한 전체 코드를 게시 할 수 있습니까? –
_ "NuGet을 업데이트하는 동안 엉망이 될 때까지이 작업이 사용되었습니다. 여기에 무엇이 없습니까?"- 소스 제어. 마우스 오른쪽 버튼을 클릭 -> 실행 취소/되돌리기. – CodeCaster
프로젝트가 올바르게 빌드되고 실행됩니다. 나는 차라리 되돌리기보다는 잘못을 고치고 이해하려고 노력할 것입니다.(그리고 나는 그러한 복귀가 몇 시간 동안 나를 되돌아 놓을 것이라고 고백한다.) – Joel