0

著者が次のようにカスタムコントローラーファクトリーを作成しようとしている本の例を見ています。(CreateController メソッドのみ参照)

public IController CreateController(RequestContext requestContext,  
            string controllerName) { 

            Type targetType = null; 
            switch (controllerName) { 
                case "Product": 
                    targetType = typeof(ProductController); 
                    break; 
                case "Customer": 
                    targetType = typeof(CustomerController); 
                    break; 
                default: 
                    requestContext.RouteData.Values["controller"] = "Product"; 
                    targetType = typeof(ProductController); 
                    break; 
            } 

            return targetType == null ? null : 
                (IController)DependencyResolver.Current.GetService(targetType); 
        }

デフォルトの場合、controllerName のデフォルトのケースの値は、既存のコントローラー (製品) に設定されています。これにより、MVC フレームワークが、ユーザーが要求したコントローラーではなく、フォールバック コントローラーに関連付けられたビューを検索するようになるためです。私の質問は、Product と Customer のケースで同様の方法でビューが検索されないのはなぜですか?

4

1 に答える 1

0

カスタムコントローラーファクトリーを使用します。ただし、既定のビュー エンジンは、次の場所で検索するように構成されています。(例: Home コントローラー、Index アクション)

~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml 

~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx

カスタム ビュー エンジンを作成し、それを実装して他の場所を検索し、その方法で「フォールバック」場所を実装できます。

于 2014-04-25T10:13:52.133 に答える