UPDATE 2

Ok - So it looks like my question is changing again slightly :-)

I've realised now that UrlHelper.Action doesn't seem to correctly resolve the URL in any Area unless the area name is explicitly specified. If it's not specified it seems to return whatever area name we're currently in which makes it look like it's working from one part of the site but then the same link in another area resolves to the wrong Area name.

Either I've done something funky to make it do this or I'm not quite understanding how this Action method is meant to work.

UPDATE 1

I can make this work by doing the following:

return helper.Action("add", "product",new {area = "storemanagement"});

which changes my question slightly.

How come the MVC routing doesn't disambiguate the controllers with the same name and resolve to the one with the action method specified?

Origional post

Hey everyone,

I've created a helper method on the UrlHelper class and am having a small problem with one of the routes.

Here's the code for the helper method in question:

public static string AddProduct(this UrlHelper helper)
        {
            return helper.Action("add", "product");
        }

I basically have two controllers named "product" which are in different areas of the site. One of them in used for browsing of products and the other for management of products. Only one of the product controllers contains an action method "Add".

When I output the value of AddProduct

<%: Url.AddProduct() %>

The area name is resolved to the current area I'm browsing and not the correct area for the product controller containing the Add action method.

Is there something I need to set up in the routes? I'm not exactly sure how the routing works with UrlHelper.Action so I dunno if it's possible to do what I'm trying.

Cheers for any help.

Just to put the answer in the answer section for clarity...

You'll need to add Area to the RouteValues anytime you use UrlHelper.Action to generate a link path.

If you're in the Controller, you can stub a UrlHelper like this:

<!-- language: lang-cs -->
var httpContext = new HttpContextWrapper(System.Web.HttpContext.Current);
var requestContext = new RequestContext(httpContext, new RouteData());
var urlHelper = new UrlHelper(requestContext);

You can always get the current area like this:

<!-- language: lang-cs -->
HttpContext.Current.Request.RequestContext.RouteData.DataTokens["area"]

Finally, when using UrlHelper pass the area in the RouteValues object like this:

<!-- language: lang-cs -->
var url = urlHelper.Action("Index", "Home", new { area = "Pages" }));