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
}
You can customize the routes with whatever identifiers you like..
Edit: Here's an overview from the ASP.NET site
Just because your route uses the name "id" for the ID variable doesn't mean that you have to use the same name in your controller action methods.
For example, given this controller method...
...and this action method call from the view...
...you can use whatever naming convention you wish for the ID parameter in your controller action methods. All you need to do is resolve the new name to the default, whether it's "id", "alias", or whatever.
The above example should resolve to :
Use the [Bind] attribute:
This still works, your query string will just look like "/Controller/ByAlias?alias=something".