扩展 Asp.NET MVC3 控制器类
我是一位经验丰富的 .NET 程序员,也是一位使用 PHP 的 MVC 程序员。现在我是 MVC3 的新手,并尝试在其上构建我的第一个作品,因此我正在处理一些问题。对于初学者来说,如何扩展控制器类?有人可以指出我应该实施的指南/方法列表吗?
谢谢!
I'm a fairly experienced .NET programmer, as well as a MVC programmer with PHP. Now I'm new at MVC3 and trying to build my first work on it, so I'm dealing with a few questions. For starters, how do I extend the Controller Class? Can someone point me to a guide/list of methods I should implement?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不必实现任何方法来扩展控制器,尽管显然不这样做是相当愚蠢的。您只需继承它,并覆盖您想要更改的方法。
如果你不知道要改变什么方法,我不得不问你为什么要扩展它?
编辑:
您可能会受益于两个大型 MVC 示例应用程序:Nerddiner 和 Music Store。它们让您很好地了解如何在 MVC 中制作电子商务网站等。不要将它们视为福音,因为它们只是示例并且旨在简单。他们目前没有利用依赖注入或存储库设计等最佳实践。一些有用教程的链接:
这里有很多精彩视频。 Pluralsight 的内容非常简单
音乐商店教程应用程序
NerdDinner 教程
You don't have to implement any methods to extend the controller, although obviously it would be rather silly not to. You just inherit from it, and override the methods you wish to alter.
If you don't know what methods to alter, I have to question why you want to extend it?
EDIT:
You may benefit from the two big MVC sample applications, Nerddiner and Music Store. They give you a very good idea of how to make ecommerce sites and the like in MVC. Don't take them as gospel, because they are samples and are intended to be simple. They don't currently make use of best practices like Dependency Injection, or Repository design. Some links to useful tutorials:
Lots of good videos here. The Pluralsight stuff is pretty straight forward
The Music Store tutorial app
NerdDinner tutorial
你不需要实现任何东西,只需让你的类继承自System.Web.Mvc.Controller。通常,这样做并没有真正的好处,但在某些情况下,制作某种形式的通用自定义基本控制器类(项目中的所有控制器都可以共享)可能会有所帮助。
但在向控制器添加常用方法时要小心。将这些方法添加到应用程序的某些较低层,或者作为模型或视图模型上的辅助方法通常更有意义。
You don't need to implement anything, just make your class inherit from System.Web.Mvc.Controller. Normally there is no real benefit to doing this, but in some cases it can be helpful make some form of common custom base controller class that all controllers in your project could share.
Beware though, when adding common methods to your controllers. It often makes more sense to add these methods to some lower tier of your application, or as helpers methods on your models or viewmodels.