1

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

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

我有一个特殊的要求,比如这个:

/Incidents/SuperCompany/SearchPeople/Ko

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

我的行动宣言是:

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

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

4

1 回答 1

1

您可以定义两条路线,一条 withid和一条 withsearchTerm 如果id 应该是数字(或者您可以指定 regex constratints)并且具有与 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" } 
        );

笔记

首先定义一个有约束的。

于 2012-02-08T14:26:03.997 回答