How to specify default Area without adding area = "" to every ActionLink
Asked Answered
J

2

14

I have a large existing application built on ASP.NET MVC2 RC2.

All of my links look like this: htp//site/controller/action/id

I just added an Area called: BigBird.

Now when I'm in the BigBird area, all of my links look like this: htp://site/BigBird/controller/action/id

Problem is that none of those controllers/actions exist in my new Area. So I have to go through all of my actionlinks all over my application and put this routevalue: area = string.empty

Is there any way around this?

Jurisconsult answered 26/2, 2010 at 22:31 Comment(0)
H
12

I don't know of away around it if you are using the standard MVC methods (other than maybe overriding them to call your own version), but if you are using the ActionLink<TController> or other generic methods provided in the MvcFutures lib then you can.

The MvcFutures methods call ExpressionHelper.GetRouteValuesFromExpression(), which looks for an ActionLinkAreaAttribute on the controller to determine the area. So you can decorate your controllers in your main "area" as follows:

[ActionLinkArea("")]
[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

The action links should be generated correctly using the standard syntax:

<%= Html.ActionLink<HomeController>(c => c.Index(), "Home") %>
Hippogriff answered 8/3, 2010 at 18:59 Comment(4)
Thanks for the answer. This is exactly what I ended up doing but I didn't circle back to post it.Jurisconsult
Haha here I was adding a stupid Html.AreaActionLink ... then I saw the RouteValueDictionary GetRouteValuesFromExpression and wondered what is this ActionLinkAreaAttribute! Google it and landed here good job :PPasquale
Hi, but when we use this Area Attribute - [ActionLinkArea("")] and try to use Html.BuildUrlFromExpression<HomeController>(x=>Index()) we still get htp://site/BigBird/controller/action/id area in URL...Pooch
How do you do this for Html.BeginForm ?Matronna
H
0

You can do one of two things. You can either move/copy your controllers/actions into the proper area or write some new controllers for the new area (which is the approach I recommend), or you can write a custom route that forces the new area to the root (which I don't recommend, as it defeats the whole purpose of having areas):

routes.MapRoute(
    "BigBird_Override",                                             
    "BigBird/{controller}/{action}/{id}",                          
    new { area = String.Empty }
);
Hideout answered 27/2, 2010 at 6:17 Comment(1)
My explanation wasn't very clear. I added my BigBird controllers/models/views to the BigBird area. But the masterpage that covers both the Area and the main site, shows BigBird in all of the links back to the main site. So what it looks like I need to do is add {Area = ""} to all of my ActionLink calls. From what I can tell in the MVC source is that it infers the Area based on the area you are in. It doesn't determine that a controller is not in an Area, and therefor incorrectly adds BigBird to the Links that aren't in the BigBird Area... they aren't in any Area at all.Jurisconsult

© 2022 - 2024 — McMap. All rights reserved.