2012-02-08 56 views
1

我有以下路线在Global.asax中定义:指定ASP.NET MVC为图路线特例处理机3

routes.MapRoute(
       "IncidentActionWithId", // Route name 
       "Incidents/{companyId}/{action}/{id}", // URL with parameters 
       new { controller = "Incidents" } // Parameter defaults 
      ); 

我的请求的特殊情况,像这样的:

/Incidents/SuperCompany/SearchPeople/Ko 

在这种情况下,动作的确应该映射到SearchPeople action,comapnyId这个动作的参数,但只有当动作是SearchPeople时,Ko不应映射到动作的id参数,而是映射到searchTerm

我的行动宣言是:

[HttpGet] 
public ActionResult SearchPeople(string companyId, string searchTerm) 

我如何能实现Ko在我的操作方法映射到searchTerm参数?

回答

1

您可以定义两条路线,一个与id和一个与searchTerm如果的ID应该是数字(或者你可以指定正则表达式constratints),并有不同的类型SEARCHTERM

请参阅here如何定义约束条件。

实施例:

routes.MapRoute(
      "IncidentActionWithId", // Route name 
      "Incidents/{companyId}/{action}/{id}", // URL with parameters 
      new { controller = "Incidents" }, // Parameter defaults 
      new {id = @"\d+"} // numeric only 
     ); 

routes.MapRoute(
      "IncidentActionWithId", // Route name 
      "Incidents/{companyId}/{action}/{searchterm}", // URL with parameters 
      new { controller = "Incidents" } 
     ); 

首先定义一个与约束。