views:

188

answers:

2

I have two different areas, and I have a route in one of those areas that is specific to that area, but I need to generate a link to that route using Html.RouteLink from another area (it's how you get over into the new area) but it won't work... It doesn't seem possible to use RouteLink to routes in a different area.

What is the best way around this? Should I just define a new route in the other area and name it differently?

UPDATE (code):

In master page in the main area (I've tried it multiple ways, all of which have produced same result):

<a href="<%= Url.Action("Index", "Home", new { area = "CustomerSite", route = "CustomerSite_preview", domain = (string)ViewData["DomainName"] }, null) %>">

In the CustomerSite area registration as the first route registered:

        context.MapRouteLowercase(
            "CustomerSite_preview",
            "preview/{domain}/{controller}/{action}/{id}",
            new { area = "CustomerSite", controller = "Home", action = "Index", id = "" },
            new { isCustomerSite = new CustomerSiteRouteConstraint() },
            new string[] { "GrayHills.CarLotHosting.Web.Areas.CustomerSite.Controllers" }
        );
+1  A: 

In your route object you just need a property named area with the name of the area.

Html.RouteLink("My Link Text", 
                new { area = "MyArea", controller = "MyController" ... }, 
               null);
NickLarsen
I have that. It doesn't help.
Max Schmeling
I'm not sure if its just something with the `RouteLink` method then, but have a look at http://msdn.microsoft.com/en-us/library/ee671793(VS.100).aspx and consider using `ActionLink` instead?
NickLarsen
It's a customized route: example.com/preview/somedomain.com/ so I don't think actionlink can do that
Max Schmeling
It should be able to do that. You can specify anything you have a listed as a value in your route. If your route is for an area called preview, you could make a route like `"preview/{url}"` and when you specify the route, your object would would look something like `new { area = "preview", url = previewStringVariable }`.
NickLarsen
Just make sure you put that route before the catch all route.
NickLarsen
It is, but still doesn't work... and doesn't matter if route is in the area or in the root "area", it still doesn't work... so confused!
Max Schmeling
Looking at the comment, it seems that Preview is your area, so what is the route configurations for this area?
Ahmad
A: 

You need to change your route definition - see the answer http://stackoverflow.com/questions/1298422/asp-net-mvc-url-action-routing-error for more info.

In essence, your route should look as follows since you explicitly supply the controller name and action:

context.MapRouteLowercase(
        "CustomerSite_preview",
        "preview/{domain}/home/index/",
        new {   area = "CustomerSite"
              , controller = "Home"
              , action = "Index"},
        new { isCustomerSite = new CustomerSiteRouteConstraint() },
        new string[] { 
            "GrayHills.CarLotHosting.Web.Areas.CustomerSite.Controllers"}

The Url.Action will look like

<a href="<%= Url.Action("Index"
                        , new {domain = (string)ViewData["DomainName"] }
                        , null) %>">

which will result in a Url like http://localhost:56291/preview/somedomainname/home/index

Ahmad