使用Twitter.Bootstrap。MVC4是否可以将"null"传递给exampleelayoursroute .config中的客户控制器:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapNavigationRoute<HomeController>("Home Page", c => c.Index());
routes.MapNavigationRoute<CustomerController>("Customer", null) <-- pass null here
.AddChildRoute<CustomerController>("List", c => c.Index())
.AddChildRoute<CustomerController>("Add", c => c.Create())
;
}
我得到一个错误:对象引用未设置为NavigationRouteconfigureationExtensions.cs文件中的对象实例:
public static NamedRoute ToDefaultAction<T>(this NamedRoute route, Expression<Func<T, ActionResult>> action,string areaName) where T : IController
{
var body = action.Body as MethodCallExpression; <--- Error here
你不能给同一个控制器/动作添加链接:
routes.MapNavigationRoute<CustomerController>("Customer", c => c.Index())
.AddChildRoute<CustomerController>("List", c => c.Index())
或者你得到错误:{"名为'Navigation-Customer-Index'的路由已经在路由集合中。路由名不能重复。rnParameter name: name"}
到目前为止,我唯一的解决方法是在控制器中添加第二个重复的Action,并将其命名为Index2(例如):public ActionResult Index()
{
return View(db.Customers.Where(x => x.UserName == User.Identity.Name).ToList());
}
public ActionResult Index2()
{
return View(db.Customers.Where(x => x.UserName == User.Identity.Name).ToList());
}
有没有比重复代码或添加不必要的操作更好的方法?
谢谢,马克
我发现问题是全局的语句。asax文件:
BootstrapSupport.BootstrapBundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
BootstrapMvcSample.ExampleLayoutsRouteConfig.RegisterRoutes(RouteTable.Routes);
BootstrapSupport.BootstrapBundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
BootstrapMvcSample.ExampleLayoutsRouteConfig.RegisterRoutes(RouteTable.Routes);
由于安装了1.09并卸载了Twitter Bootstrap Nuget包,我在Global中找到了条目。要重复的ax文件。删除这些重复条目是有效的。对于这两个调用,您至少需要一个条目。
进入navigationroutecconfigurationextension .cs。找到一个比这更好的方法,但这个hack应该使它工作(它只是一个证明)。问题是添加两个名称相同的路由,并且名称是由路由生成的,而不是显示名称。
public static NavigationRouteBuilder AddChildRoute<T>(this NavigationRouteBuilder builder, string DisplayText, Expression<Func<T, ActionResult>> action,string areaName="") where T : IController
{
var childRoute = new NamedRoute("", "", new MvcRouteHandler());
childRoute.ToDefaultAction<T>(action,areaName);
childRoute.DisplayName = DisplayText;
childRoute.IsChild = true;
builder._parent.Children.Add(childRoute);
//builder._routes.Add(childRoute.Name,childRoute);
builder._routes.Add(Guid.NewGuid().ToString(), childRoute);
return builder;
}