4

I have an action in controller like:

public ActionResult Index(string ssn)
{
}

and default route values: {controller}/{action}/{id}

I don't want use url like /Home/Index?ssn=1234. I want use like /Home/Index/1234. But I also don't want to add new route values for ssn parameter (or custom model binder).

Is there some complete attribute, like [ActionName] but for parameters?

Something like this:

public ActionResult Index([ParameterBinding("id")] string ssn)
{
}
4

1 回答 1

1

正如 Darin & Rumi 提到的 - 没有内置属性,但是您可以使用单个路由上的RouteCollection.MapRoute约束参数通过单个新路由实现相同的效果(跨多个控制器/动作)。

以下路由配置会将“SSN”路由应用于 Foo 或 Bar 控制器,任何其他控制器将通过默认路由。

routes.MapRoute(
    name: "SSN",
    url: "{controller}/{action}/{ssn}",
    defaults: new { controller = "Foo", action = "Index" },
    constraints: new { controller = "(Foo|Bar)", action = "Index" }
);
// default route
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

编辑:或者,您可以使用似乎支持您最初要求的ActionParameterAlias库。

于 2013-10-03T14:29:17.690 回答