How to specify default Area without adding area = “” to every ActionLink

前端 未结 2 1313
余生分开走
余生分开走 2021-02-13 10:27

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:

2条回答
  •  执念已碎
    2021-02-13 11: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 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(c => c.Index(), "Home") %>
    

提交回复
热议问题