2011-07-25 41 views
2

我有一個問題。昨天我開始學習ASP.NET MVC3,並創建了一個博客,在這裏我可以添加,編輯,刪除帖子並評論這些帖子。在ASP.NET MVC3中設計控制器的正確方法

但我覺得我想要設計它不同,因爲我認爲我做錯了。

我有兩個控制器。 HomeController和BlogController,我認爲這是非常錯誤的,因爲我不/添加博客,我添加發表/評論。另外我希望我的網站不僅來自博客本身。

我想要什麼在這種情況下

/Controllers 
    HomeController.cs 
    /Blog 
     PostController.cs /* IndexAction - Show list of entries; DetailsAction - Show only one entry with comments under */ 
     CommentController.cs /* ListAction - Lists comments (not sure how to list them in Blog controller with PartialView yet...); AddAction - Add comment within Details of Post (again not sure if I can do this) */ 
    /SomeOtherThing 
     SomeOtherController.cs /* Some random data from database like greeting in index page or whatever else not related to blog in any way */ 
    /Admin 
     BlogController.cs /* For edit/delete blog data like comments/posts */ 
     SomeOtherController.cs /* For editing the other thing, not sure what for now */ 
     AccountsController.cs /* For editing accounts and such... */ 

我認爲,我希望我的控制器層次結構,看起來像這樣做,但我不知道這是做這件事的正確方法。我寧願開始學習正確的方式,然後在我的第一個項目上做錯誤的方式,然後重新學習如何正確地做到這一點。

另外,關於這些評論,讓他們在單獨的控制器是好主意嗎?因爲我想在後期操作中添加/顯示。例如localhost/blog/post/1將顯示文章+表格以添加評論+評論列表。但我不知道該怎麼做,但雖然.. :)

回答

1

當有少量控制器時,將所有控制器放在一個文件夾中。如果您願意,您可以通過在global.asax文件中指定路由來「模擬」層次結構。見this

當您預測站點中有大量​​控制器時,請使用'區域'。區域提供了一種將大型MVC Web應用程序分成更小的功能組的方法。一個區域實際上是一個應用程序內部的MVC結構。另見here

0

當處理具有可輕鬆管理的控制器數量的項目時,我通常將它們全部留在根控制器文件夾中。我一直參與的團隊通常會讓項目的規模和複雜程度決定我們是否需要創建一個「控制器層次結構」。這就是說,如果您發現使用您定義的層次結構來概念化您的項目更容易,那就去做吧。

我同意你的意見,將意見轉移到他們自己的控制器中,因爲他們是一個獨立的實體,可以在「博客文章」之外進行設計和測試。

相關問題