如何在ASP.NET MVC3中配置区域

Imr*_*rul 36 c# asp.net asp.net-mvc asp.net-mvc-areas asp.net-mvc-3

有谁知道如何在ASP.NET MVC3中配置区域.我在这里读了一篇关于区域的文章.但那篇文章并非基于MVC3.在MVC3没有命名的功能MapRootAreaRouteCollection routes其中在Global.asax中被发现

routes.MapRootArea("{controller}/{action}/{id}", 
                 "AreasDemo", 
                  new { controller = "Home", action = "Index", id = "" });
Run Code Online (Sandbox Code Playgroud)

当我使用MVC3创建一个新区域时,我得到了一个继承自AreaRegistration以下区域的类:(这里的博客是区域名称)

public class BlogsAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Blogs";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Blogs_default",
            "Blogs/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

有谁请帮助我如何在MVC3中配置区域.任何类型的链接也会有所帮助.

Dar*_*rov 40

右键单击Web项目,然后选择添加 - >区域......然后键入区域和Visual Studio的名称将是生成所有必要的类,其余的照顾.例如,区域注册可能如下所示:

public class AreasDemoAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "AreasDemo";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "AreasDemo_default",
            "AreasDemo/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

Application_StartGlobal.asax所需要的一切是:

AreaRegistration.RegisterAllAreas();
Run Code Online (Sandbox Code Playgroud)

  • 你使用错误的重载,尝试这个:@ Html.ActionLink("博客","索引","BlogHome",新的{area ="Blogs"},null) (12认同)

小智 5

您可以在根和区域中使用相同的控制器名称,只需定义它即可.

在global.asax中,添加routes.maproute的最后一行,如下所示

 routes.MapRoute(
      "Default", // Route name
       "{controller}/{action}/{id}", // URL with parameters
       new { controller = "Home", action = "Index", id = UrlParameter.Optional },// Parameter defaults
       new[]{"YourNameSpace.Controllers"}
  );
Run Code Online (Sandbox Code Playgroud)

另外,在ares/????? AreaRegistration.cs文件中添加控制器的名称

 context.MapRoute(
        "Membership_default",
        "Membership/{controller}/{action}/{id}",
         new { controller= "Home", action = "Index", id = UrlParameter.Optional }
      );
Run Code Online (Sandbox Code Playgroud)