Advanced Scenarios. Q & A

How can I specify a route name?

    [Url("products/{category?}")]
    public ActionResult Home(string category = null) { ... }

Standart Routes Mapping mechanism doesn't allow omitting Route Name values. How come it is allowed here?

Why to include Name when most of the time you won't use it? If you ommit route name, randomly generated Guid string will be assigned to it.

What if my controller classes located inside another assembly?

You can pass this assembly as an optional parameter into MapRoutes method:

    routes.MapRoutes(assembly);

What advantages this approach have in comparison to storing route information in .xml files, database or in Global.asax.cs itself?

This way you can (1) always see which concrete URL is mapped to the action method you are editing; (2) modify it if needed without searching for it in an outside location.
(3) Remember that you had to write comments like // GET: /products/{category} above action methods for the same reason that you need to know at any given point in time which URL is going to be used for your action method. With Attribute Based Route Mapper that kind of comments won't be needed anymore. (4) You don't have to specify controller's and method's names explicitly.

Is there a way to specify which routes have higher priority?

Yes. There is a Rank number property in the Route attribute. Here is how you can use it:

    [Url("products/{category?}", Order = 2)]
    public ActionResult Home(string category) { ... }

    [Url("products/promo", Order = 1)]
    public ActionResult Promo() { ... }

A route with Rank number = 1 will be on top of the list.


Back to Home Page

Last edited Feb 19, 2010 at 2:26 PM by koistya, version 20

Comments

No comments yet.