我使用WEB API创建了一个Web服务。
我正在使用此路由配置
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
我的解决方案包括两个控制器(ProductController
和DetailController
)
因此,当我想调用引用GetDetails方法的WS(位于DetailController中)时,我必须使用这样的URL:
http://localhost/api/Details/GetDetails/?id=4
对于相同的请求,是否有一种方法可以使用此URL:
http://localhost/api/Product/GetDetails/?id=4
让DetailController中的GetDetails方法?
答案 0 :(得分:4)
实际上你的网址应该是:
http://localhost/api/Details/4
http://localhost/api/Products/4
和您的控制人员:
public class DetailsController: ApiController
{
public HttpResponseMessage Get(int id)
{
...
}
}
和
public class ProductsController: ApiController
{
public HttpResponseMessage Get(int id)
{
...
}
}
现在那是RESTful。