2012-02-08 33 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" } 
     ); 

首先定義一個與約束。