2013-06-25 65 views
1

在我的網絡API我試圖得到2獲取方法.one是與參數和一個是沒有參數。web api路由不能用於兩個Get方法?

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方法返回value.but第二GET方法與參數給error.pls給一些suggession.advance感謝

回答

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應該叫GetAll(int)。有關更多信息,請參見this

+0

但我想在我的控制器中使用多個帖子,比如2個帖子或者獲得.so,那時我想添加像「routeTemplate:」api/{controller}/{action}/{id}這樣的動作關鍵詞。 「right.if我不使用動作關鍵字它的未來,但如果我添加該動作關鍵字ts不會來.. – nichu09

+0

您可以使用HTTP動詞的REST樣式映射到行動方法,就像我已經顯示,這是默認。或者,你可以像使用RPC風格的映射一樣使用MVC,你可以使用routeTemplate:「api/{controller}/{action}/{id},但是在這種情況下,你必須在URI中指定動作如'http: // localhost:/api/yourcontroller/get'或'http:// localhost:/api/yourcontroller/getall'。 – Badri

+0

是的,我已經做過了。我只用方法名稱(動作名稱)調用這個webapi,但它調用了所有沒有參數的get方法。參數不是調用。給出錯誤,如方法名稱不存在或刪除。 – nichu09