开发者

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

开发者 https://www.devze.com 2022-12-21 04:47 出处:网络
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 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 t开发者_运维百科here any way around this?


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") %>


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 }
);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号