web api路由不适合两个Get方法?

时间:2013-06-25 12:27:12

标签: asp.net-web-api

在我的网页api中,我试图获得2个Get方法.one是参数,一个是没有参数。

 public HttpResponseMessage Get()
    {}  
 public HttpResponseMessage GetAll(int id)
    {}

我这样的路线

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id =  UrlParameter.Optional }

第一个get方法是返回值。但第二个get参数的方法是给出错误。请给出一些suggession.advance谢谢

1 个答案:

答案 0 :(得分:1)

你所展示的是MVC。 Web API的Visual Studio项目模板在App_Start下的WebApiConfig.cs中创建了类似的东西:

routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

有了这个,http://localhost:<port>/api/yourcontroller上的GET请求应该调用Get(),而http://localhost:<port>/api/yourcontroller/123上的GET应该调用GetAll(int)。有关详细信息,请参阅this