MVC4:Twitter bootstrap MVC4示例包。基于用户角色的导航中的子路径

时间:2013-06-14 14:06:17

标签: asp.net-mvc asp.net-mvc-4 twitter-bootstrap asp.net-mvc-routing

今天我问了很多问题,但无论如何, 我在我的默认mvc4空项目中安装了一个名为Twitter bootstrap for asp.net mvc 4 sample的包。说实话,它看起来比默认模板好多了。 安装此模板会添加一些控制器,css文件等。 它添加了ExampleLayoutsRouteConfig.cs 看起来像这样

 public class ExampleLayoutsRouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapNavigationRoute<HomeController>("Customers", c => c.Index());
            routes.MapNavigationRoute<AccountController>("Hardware", c => c.Login());
            routes.MapNavigationRoute<ExampleLayoutsController>("Profile", c => c.Starter())
                  .AddChildRoute<ExampleLayoutsController>("Change Password", c => c.Marketing())
                  .AddChildRoute<AccountController>("Add User", c => c.Register())
                  .AddChildRoute<ExampleLayoutsController>("Logout", c => c.SignIn())
                ;
        }
    }

使我的导航菜单看起来像这样 enter image description here

是否可以根据用户的角色访问我的部分路线或子路线? 像

这样的东西
routes.MapNavigationRoute<ExampleLayoutsController>("Profile", c => c.Starter())
                  .AddChildRoute<ExampleLayoutsController>("Change Password", c => c.Marketing())
                  //can only be accessed by admin
                  .AddChildRoute<AccountController>("Add User", c => c.Register())
                  .AddChildRoute<ExampleLayoutsController>("Logout", c => c.SignIn())
                ;

3 个答案:

答案 0 :(得分:0)

似乎使用filtertokenAdministrationRouteFilter()来确定该项目是否可见:

 routes.MapNavigationRoute<HomeController>("Administration Menu", c => c.Admin(), "",
     new NavigationRouteOptions { HasBreakAfter = true, FilterToken = "admin"});

https://github.com/erichexter/twitter.bootstrap.mvc/blob/master/src/Bootstrap/App_Start/ExampleLayoutsRouteConfig.cs

public class AdministrationRouteFilter :INavigationRouteFilter
    {
        // an excercise for the reader would be to load the role name 
        // from your config file so this isn't compiled in, or add a constructor
        // that accepts a role name to use to make this a more generic filter
        private string AdministrationRole = "admin";

        public bool ShouldRemove(System.Web.Routing.Route navigationRoutes)
        {
            if (navigationRoutes.DataTokens.HasFilterToken())
            {
                var filterToken = navigationRoutes.DataTokens.FilterToken();
                var result = !HttpContext.Current.User.IsInRole(AdministrationRole) && filterToken == AdministrationRole;
                return result;
            }

            return false;

        }
    }

https://github.com/erichexter/twitter.bootstrap.mvc/blob/master/src/Bootstrap/NavigationRouteFilterExamples/AdministrationRouteFilter.cs

答案 1 :(得分:0)

另一种方法是手动插入引导程序。添加到〜/ Scripts目录和bootstrap.js文件bootstrap.mim.js。并且,在〜/ Content-responsive.css中的bootstrap文件,bootstrap-responsive.min.css,bootstrap.css,bootstrap.min.css。 准备好只使用前端框架提供的所有功能。

答案 2 :(得分:-3)

如果错误仍然存​​在,请尝试卸载程序包管理器控制台,然后运行该项目。 卸载 - 包twitter.Bootstrap.MVC4 和我一起工作。