使用專案範本實作 ASP.NET MVC Sitemap 及 Roles

Step01.新增一個含有identity的專案範本
Step02.將專案的identity角色功能打開

在App_Start的IdentityConfig.cs 加入

//增加角色管理員相關的設定
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
    public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore): base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
    }
 }

在App_Start的 Startup.Auth.c s的 public void ConfigureAuth(IAppBuilder app) 加入

//增加角色的OwinContext
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

以上這兩個步驟,專案的identity 就具備Role的功能了

Step03.Install-Package MvcSiteMapProvider.MVC5 -Version 4.6.22

Step04. 編輯 Mvc.sitemap 的內容

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0"
xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0 MvcSiteMapSchema.xsd">

  <mvcSiteMapNode title="Home" controller="Home" action="Index">
    <mvcSiteMapNode title="首頁" controller="Home" action="Index"/>
    <mvcSiteMapNode title="關於" controller="Home" action="About"/>
    <mvcSiteMapNode title="聯絡我們" controller="Home" action="Contact"/>
  </mvcSiteMapNode>
</mvcSiteMap>

Step05. 編輯 _Layout.cshtml 的內容,將原選單內容換成Mvc.sitemap的內容

<ul class="nav navbar-nav">
@Html.MvcSiteMap().Menu(false, true, false) 
</ul>

這時候你可以執行看看,選單應該還是有些不一樣。

Step06. 編輯Views/Shared/DisplayTemplates/MenuHelperModel.cshtml 的內容

@model MvcSiteMapProvider.Web.Html.Models.MenuHelperModel
@using System.Web.Mvc.Html
@using MvcSiteMapProvider.Web.Html.Models

@foreach (var node in Model.Nodes)
{
  <li>
    @Html.DisplayFor(m => node)
    @if (node.Children.Any())
    {
        @Html.DisplayFor(m => node.Children)
    }
  </li>
}

這時候你可以執行看看,選單應該是一樣了。

Step07.在Web.config 增加 Security 的宣告

<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="MvcSiteMapProvider_IncludeAssembliesForScan" value="DemoMVCSitemap" />
<add key="MvcSiteMapProvider_UseExternalDIContainer" value="false" />
<add key="MvcSiteMapProvider_ScanAssembliesForSiteMapNodes" value="true" />
//增加這一行
<add key="MvcSiteMapProvider_SecurityTrimmingEnabled" value="true"/>
</appSettings>

Step08.註冊兩個帳號,角色分別為 “Admin” 和 “Manager”,在用這兩個帳號登入看看,是否選單會跟著角色不一樣而變化

public class HomeController : Controller
{
    [AllowAnonymous]
    public ActionResult Index()
    {
        return View();
    }
    [Authorize(Roles = "Admin")]
    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";
        return View();
    }
    [Authorize(Roles = "Manager")]
    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";
        return View();
    }
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *