學用MVC4做網站六:后台管理


后台管理部分打算用一個單獨的區域。主要進行網站的設置和管理,初步設想實現功能如下:

image

首先建立區域Admin。

在項目上點右鍵->添加->區域。輸入名稱“Admin”確定。

image

創建后新增的目錄

image

從圖中可以看到,區域有自己的模型。控制器、視圖和路由,就像一個子項目。

雙擊打開ControlAreaRegistration.cs

更改代碼如下:

using System.Web.Mvc;

namespace Ninesky.Areas.Admin
{
    public class ControlAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional } ,    
                new string[] { "Ninesky.Areas.Admin.Controllers" }
            );
        }
    }
}

同樣,RouteConfig.cs文件的默認路由也要加上命名空間,改完后的樣子

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}/{page}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, page = UrlParameter.Optional },
                namespaces: new string[] { "Ninesky.Controllers" }
                );

今天就寫這兒。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM