、私たちは、カスタムConstraintImplementationを使用することができます。
public class MyRouteConstraint : IRouteConstraint
{
public readonly IList<string> KnownActions = new List<string>
{ "Send", "Find", ... }; // explicit action names
public bool Match(System.Web.HttpContextBase httpContext, Route route
, string parameterName, RouteValueDictionary values
, RouteDirection routeDirection)
{
// for now skip the Url generation
if (routeDirection.Equals(RouteDirection.UrlGeneration))
{
return false; // leave it on default
}
// try to find out our parameters
string action = values["action"].ToString();
string id = values["id"].ToString();
// id and action were provided?
var bothProvided = !(string.IsNullOrEmpty(action) || string.IsNullOrEmpty(id));
if (bothProvided)
{
return false; // leave it on default
}
var isKnownAction = KnownActions.Contains(action
, StringComparer.InvariantCultureIgnoreCase);
// action is known
if (isKnownAction)
{
return false; // leave it on default
}
// action is not known, id was found
values["action"] = "Index"; // change action
values["id"] = action; // use the id
return true;
}
と(デフォルト1前 - の両方が提供されなければならない)ルートマップ、次のようになります。
routes.MapRoute(
name: "DefaultMap",
url: "{controller}/{action}/{id}",
defaults: new { controller = string.Empty, action = "Index", id = string.Empty },
constraints: new { lang = new MyRouteConstraint() }
);
合計mary:この場合、「アクション」パラメータの値を評価しています。
- 1)アクションと2)idが提供されている場合は、ここでは扱いません。
- これが(リストまたは反映された)既知の操作であるかどうか。
- アクション名が不明な場合のみ、ルート値を変更してください:アクションを「インデックス」に設定し、アクション値をIDに変更してください。
注:
1)RouteConfigで、私はあることを、これを置く:action
名前とid
値は一意である必要があります...これは私のために働いていた何これは
RouteTableで経路をどのように定義しましたか? –
@FelipeOriani私はどちらか一方を作成すると、他のものを隠すことになります。 –