什么是ASP.NET MVC使用多个控制器类的优势在哪里? [英] What is the advantage of using multiple Controller classes in ASP.NET MVC?

查看:99
本文介绍了什么是ASP.NET MVC使用多个控制器类的优势在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚学ASP.NET MVC的基础知识,我想知道的好处是打破了网站之间的逻辑多个控制器而不是只具有运行整个网站的单一控制器类,除了简单地组织code什么更好。 (在我看来,只是后者的利益不应该由于关注点分离足以影响通过URL最终用户:网站的实施细节不应该反映在网站使用的网址,不是吗?)

I'm just learning the basics of ASP.NET MVC and am wondering what the benefit is in breaking up website logic amongst multiple controllers versus just having a single Controller class that runs the whole website, besides simply organizing code better. (In my opinion, just the latter benefit should not be enough to affect the end user via the url due to separation of concerns: the implementation details of the site should not be being reflected in the urls the site uses, no?)

在控制器我一直在阅读显示不同的控制器对于像产品或用户或邮报的一些例子。这些显然对应的对象之后,可以对这些采取的行动的类(在URL看,现在我明白了stackoverflow.com/questions/ask)。

Some examples on Controllers I've been reading show different controllers for things like "Product" or "User" or "Post". These clearly correspond to classes of objects followed by actions that can be taken on those (looking at the url right now I see stackoverflow.com/questions/ask).

有网站分割开的控制器类,如QuestionsController而不只是有一个默认的控制器,并在其内处理这些操作,例如stackoverflow.com/ask-question(除了它看起来有点丑陋)的优势。

Is there an advantage of splitting up the website into separate controller classes like QuestionsController versus just having a single default controller and handling these actions within it, for example stackoverflow.com/ask-question (besides it looking slightly uglier).

我问,因为我没有让我的网站的RESTful(我看着这一点,但认为它太受限制)特别感兴趣,并转而青睐查询字符串参数来传递有关请求的信息。因此,分裂了一个网址进入控制器和动作没有任何意义,我,因为动作和班级信息的概念将被重新$ P $查询字符串psented。

I ask because I'm not particularly interested in making my website RESTful (I looked into it a bit but deemed it too limiting) and instead favour query string parameters to pass information about a request. Therefore, the concept of splitting a url up into controller and action doesn't make sense to me, since the action and class information will be represented in the query string.

最后,我还是preFER喜欢的网址更简洁的外观www.mysite.com/about与www.mysite.com/home/about(这是什么意思,甚至?),再次导致我不知道是什么多个控制器的点到底是什么。

Finally, I much prefer the simpler look of urls like www.mysite.com/about versus www.mysite.com/home/about (what does that even mean?), again leading me to wonder what the point of multiple controllers really is.

推荐答案

ASP.NET MVC的美丽来自于它使关注这么简单分离的事实。与ASP.NET Web表单,其中每一页基本上是视图的的控制器,在ASP.NET MVC中,你可以抽象模型的逻辑成单独的问题或功能组。这是有道理的有一个处理一切与产品,因​​为这样你可以在你的应用程序到那些独立检验和维护制服团体隔离每个组相关功能的ProductsController的。

The beauty of ASP.NET MVC comes from the fact that it makes separation of concerns so simple. Unlike ASP.NET Webforms where each page is essentially the View and the Controller, in ASP.NET MVC you can abstract your model logic into separate concerns or 'feature groups'. It makes sense to have a ProductsController that handles everything to do with Products because then you can isolate each set of related functionality in your application into uniform groups that are independently testable and maintainable.

有一个DoEverythingController从根本上违背了MVC背后的理由,因为它团块所有的模型逻辑连成code的一个巨大的一碗意大利面条,而不是保持它的整洁有序。此外,有一个控制器,做的一切,没有特别的面向对象,类似于一个更加程序的方式就像它有一些中央的functions.php或类似的,做的家居许多(老年)PHP网站开发的。它的混乱,无序。

Having a DoEverythingController fundamentally defeats the reasoning behind MVC because it clumps all of the model logic together into one giant spaghetti bowl of code, as opposed to keeping it neat and organized. Furthermore, having a Controller that does everything is not especially object-oriented and resembles a more procedural approach to development like many (older) PHP websites which have some central "functions.php" or similar that does everything. It's messy and disorganized.

在问候你的最后一点,在MVC路由引擎允许你建立你的路线,但是你想给控制器动作。 ControllerX的关于()的行动和ControllerY的联系()动作既可以有根的URL喜欢/和/接触,只要你相应地定义的路由。

In regards to your final point, the routing engine in MVC allows you to construct your routes to given controller actions however you want. The About() action of ControllerX and the Contact() action of ControllerY can both have root URLs like /about and /contact so long as you define the routes accordingly.

修改(太长的评论)

一般而言,控制器类将是pretty薄尽可能code和逻辑而言。精心设计的控制器往往会交班更复杂的操作,像这样的失败的表面面积仍然很小,从数据存储检索数据,以某种服务。尽管大多数控制器的薄,您的网站越大,更多的操作就需要发生,而笨重的通用控制器将成为。即使在非MVC的情景,的巨大的code文件吸吮维护和更新(如上面的的functions.php为例)。

Generally speaking, the controller classes will be pretty thin as far as code and logic is concerned. Well designed controllers will often hand off more complex operations like retrieving data from the data store to some kind of service so that the surface area for failure remains small. Despite the 'thinness' of most controllers, the larger your site is, the more operations will need to occur and the bulkier your universal controller is going to become. Even in non-MVC scenarios, huge code files suck to maintain and update (like "functions.php" above, for example).

如果你使用MVC开发场地小,仅限于只有少数更多或更少的静态页面,然后使用一个控制器为所有这些可能是一个合理的方法,但如果你正在构建一个可扩展的应用程序将随时间而改变这将是真正的失败主义者放弃使用多个控制器。

If the site you're developing with MVC is small and limited to only a few more-or-less static pages, then using a single controller for all of them might be a reasonable approach, but if you are constructing a scalable application that will change over time it would be truly defeatist to forgo the use of multiple controllers.

这篇关于什么是ASP.NET MVC使用多个控制器类的优势在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆