2013-06-18 8 views
18

私のエリアは以下の通りです。該当する部分のみが強調表示されます。サブフォルダ内のコントローラ

enter image description here

ルートテーブル

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute(
     "SubFolder", // Route name 
     "SubFolder/ChildController", 
     new { controller = "ChildController", action = "Index" }, 
     new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" }); 


    routes.MapRoute(
     "Default", // Route name 
     "{controller}/{action}", // URL with parameters 
     new { controller = "Home", action = "Index" } // Parameter defaults 
    ); 
} 

This only works when the url is like this 
localhost:2474/SOProblems/ChildController/index 

This does not works when the url is like this 
localhost:2474/SOProblems/SubFolder/ChildController/index 

あなたが欠けているものを教えていただけますか?

+0

「機能しません」とは正確に何かを意味しますか? – TGlatzer

+0

@ Grumbler85 - 最後に試したURLは404、 –

+0

を返すBacktickはここには "SubFolder'/ChildController"のみがありますか?サブフォルダ内のネームスペースが正しく設定されていることを確認してください。 – TGlatzer

答えて

14

これは動作しますしないこの はlocalhost:/ ChildController /インデックスノーマルだ

2474/SOProblems /サブフォルダ。ルートパターンはSubFolder/ChildControllerで、SubFolder/ChildController/indexではありません。それに加えて、誤った場所であなたのルートを定義しました。メインルート定義で定義し、エリアルート定義では定義しませんでした。だからあなたのメインルートからカスタムルートの定義を取り除くと(あなたSOProblemsルートが登録されなければならないところである)SOProblemsAreaRegistration.csファイルに追加します。また

public override void RegisterArea(AreaRegistrationContext context) 
{ 
    context.MapRoute(
     "SubFolderRoute", 
     "SOProblems/SubFolder/ChildController", 
     new { controller = "ChildController", action = "Index" }, 
     new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" } 
    ); 

    context.MapRoute(
     "SOProblems_default", 
     "SOProblems/{controller}/{action}/{id}", 
     new { action = "Index", id = UrlParameter.Optional } 
    ); 
} 

あなたのルートパターン(SOProblems/SubFolder/ChildController)が持っていないので、アクション名を指定する可能性がある場合、このコントローラでは1つの操作しかできず、この場合は登録済みのデフォルトアクション(index)になります。

あなたはこのコントローラの以上のアクションを持っていると、まだインデックスは、あなたのルートパターンにすることを含める必要があり、デフォルトの1になりたかった場合:

context.MapRoute(
    "SubFolder", 
    "SOProblems/SubFolder/ChildController/{action}", 
    new { controller = "ChildController", action = "Index" }, 
    new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" } 
); 

どちらの場合も、あなたの主なルート定義がデフォルト値で残っている可能性があり:

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute(
     "Default", 
     "{controller}/{action}", 
     new { controller = "Home", action = "Index" } 
    ); 
} 
+2

"RegisterArea"はどこから来たのですか?私はゼロの結果でソリューション全体を検索しました。 – usefulBee

+0

@usefulBee "RegisterArea"は、Visual Studioが新しい_Add> Areaを足場にするときに発生します。... – Jasen

5

あなたの新しいルート "SubFolder"には、あなたの場合( "あなたのケースでは")あなたのルートに行動を含めることの可能性は含まれていません。

あなたのサンプルURLは

localhost:2474/SOProblems/SubFolder/ChildController/index 

のようなルートと一致するようにしようとするウォンツ:

"SubFolder/ChildController/{action}" 

をしかし、それは勝ったので、あなたは、あなたのルートに「{アクション}」は含まれていませんあなたのルートに合っていない。その後、デフォルトルートが試行されますが、これは明らかに失敗します。あなたのルートに "{アクション}を" 追加

試してみてください。

routes.MapRoute(
    "SubFolder", // Route name 
    "SubFolder/ChildController/{action}", 
    new { controller = "ChildController", action = "Index" }, 
    new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" }); 

またはテストURLオフ "インデックス" を取ります。 URLは以下のようであるとき

+2

これは機能しません。 –

関連する問題