30

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
}

4 Answers 4

50

Use the [Bind] attribute:

public ActionResult ByAlias([Bind(Prefix = "id")] string alias) {
    // your code here
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's the best example of using Bind that I've found yet! Is there a way to also make this work on model properties? i.e. instead of the method signature having a string parameter, it has a class as a parameter that has a property that looks like this: [Bind(Prefix = "id")]string Alias {get; set;}? That example doesn't compile... how would I modify it to compile (and also work)?
1

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

2 Comments

I guess you're saying it still works meaning if I had actually used Html.ActionLink to create my link, then it would create a URL as you've described... what if I didn't do that and just hit the URL /Controller/ByAlias/aliashere -- can I somehow bind the string alias parameter to the "id" piece defined in the route?
On my MVC version, that did not work. It strictly expected /{id} - could not provide ?alias=val
0

You can customize the routes with whatever identifiers you like..

routes.MapRoute(
  "Default",                                              // Route name
  "{controller}/{action}/{alias}",                           // URL with parameters
  new { controller = "Home", action = "Index", alias = "" }  // Parameter defaults
);

Edit: Here's an overview from the ASP.NET site

1 Comment

I sure can, but if I still want the original /Home/Index/id to work, I can't very well define /Home/Index/alias right after it and expect it to ever get hit.
0

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...

public Controller MailerController
{
    public ActionResult Details(int mailerID)
    {
        ...
        return View(new { id = mailerID });
    }
}

...and this action method call from the view...

<%= Html.ActionLink("More Info", "Details", new { mailerID = 7 }) %>

...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 :

<a href="/Mailer/Details/7">More Info</a>

2 Comments

For me (in MVC 1.0) mailerID will always result in null if I hit /Mailer/Details/7. As soon as I change int mailerID to int id (as per the Route) it works fine.
using id allows you to generalize your routes. Without this, you would need one route for every naming variation of id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.