5

Twitter.Bootstrap.MVC4 を使用すると、ExampleLayoursRoute.config でCustomer コントローラーに「null」を渡すことができます。

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' という名前のルートは既にルート コレクションにあります。ルート名は一意である必要があります。\r\nパラメータ名: 名前」}

これまでの私の唯一の回避策は、コントローラーに 2 つ目の複製アクションを追加し、それに 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());
    }

コードを複製したり、不要なアクションを追加したりするよりも良い方法はありますか?

ありがとう、マーク

4

2 に答える 2

0

NavigationRouteConfigurationExtension.cs に移動します。これよりも良い方法を見つけてください。ただし、このハックでうまくいくはずです (これは単なる証拠です)。問題は、同じ名前の 2 つのルートを追加することであり、名前は表示名ではなくルートから生成されます。

    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;
    }
于 2013-12-27T22:56:26.963 に答える