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

后端 未结 3 1533
眼角桃花
眼角桃花 2021-02-12 09:37

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

相关标签:
3条回答
  • 2021-02-12 10:01

    @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.

    0 讨论(0)
  • 2021-02-12 10:04

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

    0 讨论(0)
  • 2021-02-12 10:21

    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.

    0 讨论(0)
提交回复
热议问题