简体   繁体   中英

MVC4 : Twitter bootstrap MVC4 sample package. child route in navigation based on user role

I'm asking lots of question today but anyways, I installed a package called Twitter bootstrap for asp.net mvc 4 sample to my default mvc4 empty project. To be honest it looks much better than default template. Installing this template adds some controller, css files, etc. It has added a ExampleLayoutsRouteConfig.cs it looks like this

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

makes my navigation menu look like this 在此输入图像描述

is it possible to access my some of my route or child route based upon role of user? Something like

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

It seems like it uses a filtertoken and AdministrationRouteFilter() to determine if the item is visible or not:

 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

Another way is to intalar Bootstrap manually. Add to ~ / Scripts directory and the files bootstrap.js bootstrap.mim.js. And, in the ~ / Content-responsive.css the bootstrap files, bootstrap-responsive.min.css, bootstrap.css, bootstrap.min.css. Ready just use all the features offered by the front-end framework.

If the error continues try to uninstall the Package Manager Console and then run the project. Uninstall-Package twitter.Bootstrap.MVC4 Worked with me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM