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

后端 未结 3 1546
眼角桃花
眼角桃花 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.

提交回复
热议问题