Why do I need an underscore for partial views in asp.net mvc

强颜欢笑 提交于 2019-12-09 07:53:35

问题


Just to distinguish between a view used inside a dialog or used in a foreach loop (customer details) ?


回答1:


You don't need an underscore. It's just a convention, and MVC is very keen on using conventions.




回答2:


Mike Brind has put this nicely in the question Why does Razor _layout.cshtml have a leading underscore in file name?:

Since layout pages in Web Pages are not intended to be served directly, they are prefixed with the underscore. And the Web Pages framework has been configured not to allow files with leading underscores in their names from being requested directly.

Besides that, I find it very helpful to use this convention to differentiate between full views and partial ones.




回答3:


@Marius Schulz gives a nice reference, but then misses the point. Yes, the underscore helps to differentiate between full views and partial ones, but more importantly, it prevents partial views from being loaded directly by their URL, which could provide some potentially ugly results! (Like no css, for starters.)

EDIT: Mystere Man is right...what was I thinking? URLs in MVC point to controller/action, not to view.

Also, it is possible to mess things up and display a partial in a seperate window, so the naming convention does not prevent that. @Marius Schulz and I had the same misinterpretation of his quote.

The leading underscore is a useful convention to differentiate full and partial views, and I will continue to use it, but is is just a convention, not a functional difference.



来源:https://stackoverflow.com/questions/10321357/why-do-i-need-an-underscore-for-partial-views-in-asp-net-mvc

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