在ASP .NET Web API中进行路由

时间:2014-02-20 16:32:11

标签: asp.net .net asp.net-mvc-4 asp.net-web-api

我使用WEB API创建了一个Web服务。

我正在使用此路由配置

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

我的解决方案包括两个控制器(ProductControllerDetailController

因此,当我想调用引用GetDetails方法的WS(位于DetailController中)时,我必须使用这样的URL:

http://localhost/api/Details/GetDetails/?id=4

对于相同的请求,是否有一种方法可以使用此URL:

http://localhost/api/Product/GetDetails/?id=4

让DetailController中的GetDetails方法?

1 个答案:

答案 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。