代码之家  ›  专栏  ›  技术社区  ›  Paul Farry

使用管理子网站进行路由的正确过程

  •  1
  • Paul Farry  · 技术社区  · 15 年前

    我正在构建我的第一个ASP.NET MVC2站点,现在我正在尝试向该站点添加一个/管理区域。

    我不希望这一区域对主要用户集可见,因此只有当您进入时才能访问 http://Intranet/Admin

    我有一个普通用户的新闻控制器,但我也需要一个管理新闻控制器,我不知道如何设置类层次结构和文件夹,以便当我添加视图时,它们在正确的位置。

    在我的global.asax.cs中,我添加了并且路由正确解析。

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
        new string[] { "Intranet.Controllers" } 
    );
    
    routes.MapRoute(
        "Admin", // Route name
        "Admin/{controller}/{action}/{id}", // URL with parameters
        new { controller = "Admin", action = "Index", id = UrlParameter.Optional }, // Parameter defaults 
          new string[] { "Intranet.Controllers.Admin" } 
    );
    

    在文件夹层次结构中,我已经设置了

    Views/
         Admin/
         News/
             ...I want the new view to go here...
    

    在控制器中

    Controllers/
        Admin/
            AdminController.cs
            NewsController.cs (this is the one i want for administration)
        NewsController.cs (this is the regular one for viewing the list, specific item etc)
    

    我面临的问题是,当我进入index上的admin/newscontroller.cs并添加视图时,它试图在/news/index.aspx而不是/admin/news/index.aspx创建视图。

    这是我的管理新闻控制器/admin->添加->控制器的代码

    namespace Intranet.Controllers.Admin
    {
        public class NewsController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
        }
    }
    

    是否有我做的不正确的事情,或者我应该更改什么,以便在添加视图时在/admin/区域目录中创建它们。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Steve Michelotti    15 年前

    /Areas
        /Admin
            /Controllers
                NewsController.cs
    etc.
    
        2
  •  0
  •   rob waminal    15 年前

     Controllers
         Admin
             AdminController.cs
             HomeController.cs
         HomeController.cs
    

    Views
        Home
            Index.aspx
    

    namespace Intranet.Controllers.Admin
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View("Home/Index");
            } 
        }
    }
    

    Views
        Admin
            Home
                Index.aspx