将操作映射到区域根目录

时间:2015-01-22 17:51:58

标签: .net asp.net-mvc-4

我有一个名为Area1的区域,我想将一个动作映射到它的根目录。例如,我有一个控制器(Controller1),其中包含一个名为Main的操作,我想将其映射到www.examplewebsite.com/Area1。

我在Area1AreaRegistration.cs中尝试了以下内容,但它没有工作:

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Area1_root",
            "Area1",
            new { controller = "Controller1", action = "Main", id = UrlParameter.Optional }
        );
    }

有没有办法用区域做到这一点?

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

context.MapRoute(
    "Area1_root",
    "Area1/{action}/{id}",
    new { action = "Main", controller="Controller1", id = UrlParameter.Optional }
);

答案 1 :(得分:0)

我知道一种带动作链接的方法。试试这样:

@Html.ActionLink("Main", "Controller1", new { area = "Area1"})