2016-08-25 17 views
-2

Iは4つemployeedetailsコントローラのメソッドを取得していますID)のURLルート注文ASP.NETのWeb APIに

4)DetailsForTeam(ID)と、コントローラは次のとおりです。私は、このような

としてURLたい

1)API /従業員の代わりに、URL /をEmployeeDetails /従業員 その後全て

2)API /詳細/ ID

3)API/TEAMINFO/ID

4)API/DetailsForTeam /用ID

は、これが私のRouteConfig.cs設定

public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 
      routes.MapRoute(
       name: "Employees", 
       url: "api/employeedetails/Employees", 
       defaults: new { controller = "EmployeeDetails", action = "Employees" } 
      ); 
      routes.MapRoute(
      name: "GetEmployeeDetailsById", 
      url: "api/employeedetails/Details/{id}", 
      defaults: new { controller = "EmployeeDetails", action = "Details", id = UrlParameter.Optional } 
     ); 

      routes.MapRoute(
      name: "GetTeamMember", 
      url: "api/employeedetails/TeamInfo/{id}", 
      defaults: new { controller = "EmployeeDetails", action = "TeamInfo", id = UrlParameter.Optional } 
     ); 

      routes.MapRoute(
      name: "GetTeamMemberById", 
      url: "api/employeedetails/DetailsForTeam/{id}", 
      defaults: new { controller = "EmployeeDetails", action = "DetailsForTeam",id = UrlParameter.Optional} 
     ); 
     } 
    } 

であり、これは私のWebApiConfig.csです

public static void Register(HttpConfiguration config) 
     { 
      config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{action}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 

私は変更する必要がありますか?

+2

移動defaultをルート(順序

config.MapHttpAttributeRoutes(); 

をそして、あなたのコントローラで問題)。次に、 'url:" api/Employees "、' etcに経路を変更します。 –

+0

私は@StephenMueckeに同意します。特定のものを上部に、一般的なものを下部に持つべきです。 –

+0

まだエラーが発生しています。要求URI「http:// localhost:60669/api/Employees」に一致するコントローラ名を指定するルートが見つかりませんでした。 – user123

答えて

0

私は属性ルーティングに行くことをお勧めします。あなたのWeb APIの設定では、追加:

[RoutePrefix("api")] 
public class EmployeeDetailsController 

そして、あなたの行動:最後に

[HttpGet] // if you are calling via GET verb 
[Route("Employees")] 
public IHttpActionResult Employees() 
{ 
} 

[HttpGet] // if you are calling via GET verb 
[Route("Details/{id}"] 
public IHttpActionResult Details(int id) 
{ 
} 

[HttpGet] // if you are calling via GET verb 
[Route("TeamInfo/{id}"] 
public IHttpActionResult TeamInfo(int id) 
{ 
} 

[HttpGet] // if you are calling via GET verb 
[Route("DetailsForTeam/{id}"] 
public IHttpActionResult DetailsForTeam(int id) 
{ 
}