后台管理部分打算用一個單獨的區域。主要進行網站的設置和管理,初步設想實現功能如下:
首先建立區域Admin。
在項目上點右鍵->添加->區域。輸入名稱“Admin”確定。
創建后新增的目錄
從圖中可以看到,區域有自己的模型。控制器、視圖和路由,就像一個子項目。
雙擊打開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" }
);
今天就寫這兒。