우리는 여기에 편안한 WebAPI 서비스를 가지고 난 다음에 대처하기 위해 내 코드 및 라우팅을 구성하기 위해 무엇을해야하는지에 대해 궁금했는데 :REST WebAPI - 작업 및 하위 컨트롤러는 어떻게됩니까?
http://myapi/customer/1/files
및
http://myapi/customer/1/files/3
그래서 I 기본적으로 고객 정보를 관리하는 고객 컨트롤러와 파일 정보를 관리하는 파일 컨트롤러가 있으며, 고객 1의 모든 파일을 원한다면 첫 번째 요청을 수행 할 수 있습니다.
는 정말 고객의 맥락에서 그것을 관리하지 않으므로 파일에 내 GET이 파일에 대한 ID에 초점을 때, 나는 그것을 과부하해야하고 할 것
http://myapi/files/?customer=1&files=all
첫 번째 해결 방법보다 깨끗하게 보입니다. 순간
나는 다음과 같은 한 : http://myapi/customer/1/files
를 보낼 때 FilesController
로 확인
config.Routes.MapHttpRoute(name: SubController, routeTemplate: "{entity}/{entityid}/{controller}/", defaults: null);
config.Routes.MapHttpRoute(name: SubControllerAndId, routeTemplate: "{entity}/{entityid}/{controller}/{id}", defaults: null);
:
이 작동하고 엔티티 ID로 엔티티로customer
및
1
에 전달
[HttpGet]
public HttpResponseMessage Get(string entity, string entityid, int id)
{
var item = "Hello " + entity + " " + entityid + " " + id;
return Request.CreateResponse(HttpStatusCode.OK, item);
}
[HttpGet]
public HttpResponseMessage Get(string entity, string entityid)
{
var item = "Hello " + entity + " " + entityid;
return Request.CreateResponse(HttpStatusCode.OK, item);
}
,하지만 그것은 최선의 해결책처럼 느껴지지 않습니다. 더 좋은 방법이 있습니까? 잘못입니까?
난 당신이 반대, HTTP를 의미 생각? –
@PedroWerneck 아니, 내가 쓴 것이 맞다. :) 고객의 ID를 중점적으로 다루기 때문에 잘못하기 전에 고객의 모든 파일을 받아 들인다. –
. 어쨌든, 당신 문제는 정말로 저에게 분명하지 않습니다. –