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

时间:2011-03-09 08:38:16

标签: c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-areas

有谁知道如何在ASP.NET MVC3中配置区域。 我在here读了一篇关于区域的文章。 但那篇文章并非基于MVC3。 在MVC3中,MapRootArea中没有名为RouteCollection routes的函数,可在Global.asax中找到

routes.MapRootArea("{controller}/{action}/{id}", 
                 "AreasDemo", 
                  new { controller = "Home", action = "Index", id = "" });

当我使用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 }
        );
    }
}

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

3 个答案:

答案 0 :(得分:40)

右键点击您的网络项目,然后选择添加 - >区域...然后键入区域的名称,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 }
        );
    }
}

并且Application_Start Global.asax只需要AreaRegistration.RegisterAllAreas();

{{1}}

答案 1 :(得分: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"}
  );

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

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

答案 2 :(得分:1)

请在下面找到如何在mvc。enter image description here

中配置区域的图片
相关问题