【问题标题】:How to Configure Areas in ASP.NET MVC3如何在 ASP.NET MVC3 中配置区域
【发布时间】:2011-03-09 08:38:15
【问题描述】:

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

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 中配置区域。任何类型的链接也会有所帮助。

【问题讨论】:

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


    【解决方案1】:

    右键单击您的 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 }
            );
        }
    }
    

    在您的Global.asax 中的Application_Start 中,您只需要:

    AreaRegistration.RegisterAllAreas();
    

    【讨论】:

    • 感谢您的回答。我如何知道该区域已正确注册?我在共享的 _layout 中使用了这个链接,但它没有带我去任何地方 @Html.ActionLink("Blog", "About", "Home", new { area = "Blog"})
    • @Imrul,从我在您的代码中可以看到,您的区域称为Blogs,而不是Blog,因此请尝试:@Html.ActionLink("Blog", "About", "Home", new { area = "Blogs" })。还要确保该区域内有HomeController
    • 抱歉,拼写错误。我想我遇到了问题,如果我将它渲染到我的 _layout 它不会生成到 Area/Controller/Action "@Html.ActionLink("Blog", "Index", "BlogHome", new { area = "博客"})"。我在 ActionLink 助手中做错了吗?我现在发现在 root 和 Area 中不可能有相同的控制器名称。为此,我不得不将 Home 重命名为 BlogHome。仅供参考 Blog 是生成的 html,使用 localhost:4135/Blogs/BlogHome/Index 会点击 BlogHome 控制器并显示页面。
    • 你使用了错误的重载,试试这个:@Html.ActionLink("Blog", "Index", "BlogHome", new { area = "Blogs" }, null)
    • 谢谢,@frennky。它就像一个魅力。请给我任何链接,我在哪里可以找到这些更改。我用谷歌搜索但没有帮助。我在哪里可以找到 ASP.NET MVC3 的所有函数/助手?
    【解决方案2】:

    你可以在根和区域中拥有相同的控制器名称,你只需要定义它。

    在你的 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 }
          );
    

    【讨论】:

      【解决方案3】:

      请看下图显示如何在 mvc 中配置区域 .

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-13
        相关资源
        最近更新 更多