Why are the controllers on ASP.NET MVC name-based?

坚强是说给别人听的谎言 提交于 2019-12-10 12:58:35

问题


In ASP.NET MVC, we're required to use the suffix "Controller" for all controllers. This seems unnecessarily restrictive - is there a technical reason for it?

I'm mostly just curious, but can see situations where more flexible naming rules could improve code organization. Couldn't the discovery of possible controller classes be easily made using reflection to search for Controller derived classes? Or require that controller classes be marked with a ControllerAttribute?


回答1:


The MVC community is heavily influenced by Ruby on Rails, which values "convention over configuration". By just naming things consistently, the application can run with zero configuration.




回答2:


One of the benefits of this convention is that it's common to have a URL segment, controller, and a model class all have the same name.

URL: /product/ Controller: Product : Controller Model: Product

This would cause a naming conflict. So we made a convention to have controller names suffixed with "Controller" to avoid this conflict. However, you can override this behavior via our extensibility APIs.



来源:https://stackoverflow.com/questions/266106/why-are-the-controllers-on-asp-net-mvc-name-based

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!