ASP.NET MVC:构建控制器 [英] ASP.NET MVC: Structuring Controllers

查看:126
本文介绍了ASP.NET MVC:构建控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在开始一个ASP.NET MVC项目,而经验是整体的一个很好的,我不是很高兴与意大利面条混乱,我的控制器都成了。我在网上看了四周(codeCampServer,等...),他们似乎都遭遇了同样的问题,其中控制器方法违反SRP(单一职责原则)pretty一致 - 如只是呈现一个控制器方法认为如果请求是一个GET,但如果它是一个POST更新模型。负责整个应用程序的多个逻辑路径现在我已经得到了控制方法 - 说它检查被点击表格上的按钮,并采取相应的行动。我可以使用JavaScript每个按钮点击不同的形式操作重定向的事,但感觉不对那里要么...的另一大问题是魔弦的扩散 - 计算机[富] =等等;长话短说,你们如何组织你的控制器逻辑?按次一个巨大的模型对象?的微控制器方法和JavaScript地段是路由器?我的目标是维护code - 作为特征得到堆在我开始下滑,这将滑坡...

So I'm embarking on an ASP.NET MVC project and while the experience has been a good one overall, I'm not quite as pleased with the spaghetti mess that my controllers have become. I've looked around online (CodeCampServer, etc...) and they all seem to suffer the same issue wherein controller methods violate SRP (single responsibility principle) pretty consistently - such as a controller method that simply renders the view if the request is a GET but updates the model if it's a POST. Now I've got controller methods responsible for multiple logical routes throughout the application - say it checks for which button was clicked on the form and acts accordingly. I could redirect each button click to a different form action using JavaScript, but something doesn't feel right there either... The other big issue is the proliferation of magic strings - ViewData["foo"] = blah; Long story short, how do you guys structure your controller logic? One giant model object per view? Lots of little controller methods and JavaScript is the router? My goal is maintainable code - as features get piled on I'm starting to slide down that slippery slope...

推荐答案

ASP.NET preVIEW 5(的 codePLEX )有一个这样的答案:的[AcceptVerbs]属性。菲尔哈克有博客文章讨论如何使用它。

ASP.NET Preview 5 (available on CodePlex) has an answer for this: the [AcceptVerbs] attribute. Phil Haack has a blog post discussion how it's used.

至于视图数据法宝的问题,这是一个有趣的问题。如果你(特别是在新的局部视图支持光)认为视图是一堆半独立的构件,然后又做了强类型的模型变得不太理想,因为几件的观点应该是相对独立的另一个。

As for the view data magic key question, it's an interesting problem. If you think of a view as being a bunch of semi-independent components (especially in light of the new partial view support), then making a strongly-typed model becomes less ideal, as the several pieces of the view should be relatively independent of one another.

这篇关于ASP.NET MVC:构建控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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