Action Parameter Naming

asp.net-mvc, asp.net-mvc-routing, c#

Solution

Use the [Bind] attribute:

public ActionResult ByAlias([Bind(Prefix = "id")] string alias) {
    // your code here
}

Problem

Using the default route provided, I'm forced to name my parameters "id". That's fine for a lot of my Controller Actions, but I want to use some better variable naming in certain places. Is there some sort of attribute I can use so that I can have more meaningful variable names in my action signatures? ``` // Default Route: routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); // Action Signature: public ActionResult ByAlias(string alias) { // Because the route specifies "id" and this action takes an "alias", nothing is bound } ```

Original source