Asp.net mvc 在ASP.NET MVC 3中为MapRoute指定特殊案例处理程序

Asp.net mvc 在ASP.NET MVC 3中为MapRoute指定特殊案例处理程序,asp.net-mvc,maproute,Asp.net Mvc,Maproute,我在Global.asax中定义了以下路线: routes.MapRoute( "IncidentActionWithId", // Route name "Incidents/{companyId}/{action}/{id}", // URL with parameters new { controller = "Incidents" } // Parameter defaults

我在Global.asax中定义了以下路线:

routes.MapRoute(
                "IncidentActionWithId", // Route name
                "Incidents/{companyId}/{action}/{id}", // URL with parameters
                new { controller = "Incidents" } // Parameter defaults
            );
我有一个特殊的请求,比如:

/Incidents/SuperCompany/SearchPeople/Ko
在这种情况下,操作确实应该映射到
SearchPeople
操作,
comapnyId
映射到此操作的参数,但只有当操作是SearchPeople时,
Ko
不应该映射到操作的id参数,而应该映射到
searchTerm

我的行动宣言是:

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

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

您可以定义两个路由,一个路由为
id
,另一个路由为
searchTerm,如果该id应该是数字的(或者您可以指定regexconstratints)对搜索词有不同的模式

了解如何定义约束

例如:

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" } 
        );

首先定义带有约束的路由。

您可以定义两个路由,一个路由带有
id
,另一个路由带有
searchTerm
,如果该id应该是数字的(或者您可以指定regexconstratints),并且与searchTerm具有不同的模式

了解如何定义约束

例如:

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" } 
        );
注 首先定义具有约束的