Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action Parameter Naming

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
}
like image 373
Langdon Avatar asked Jan 08 '10 20:01

Langdon


2 Answers

Use the [Bind] attribute:

public ActionResult ByAlias([Bind(Prefix = "id")] string alias) {
    // your code here
}
like image 74
Levi Avatar answered Nov 14 '22 02:11

Levi


This still works, your query string will just look like "/Controller/ByAlias?alias=something".

like image 33
Chris Shaffer Avatar answered Nov 14 '22 03:11

Chris Shaffer