2

我今天问了很多问题,但无论如何,我安装了一个名为Twitter bootstrap for asp.net mvc 4 sample我的默认 mvc4 空项目的包。老实说,它看起来比默认模板好得多。安装此模板会添加一些控制器、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())
                ;
        }
    }

使我的导航菜单看起来像这样 在此处输入图像描述

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

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())
                ;
4

3 回答 3

0

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

 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/ExampleLayoutsRou​​teConfig.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

于 2013-06-14T14:46:48.817 回答
0

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

于 2013-11-29T13:41:50.553 回答
-3

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

于 2013-11-25T12:56:00.533 回答