Why does Razor _layout.cshtml have a leading underscore in file name?

前端 未结 5 779
傲寒
傲寒 2020-12-07 16:21

In the default ASP.NET MVC 3 project, layout & partial cshtml files start with an underscore

  • _viewstart
  • _Layout
5条回答
  •  甜味超标
    2020-12-07 16:59

    Pages that cannot be shown by direct requests from your browser (master pages, partial views etc) have underscore (_) in the beginning of their names.

    So if you try to make the request to _Layout.cshtml (this is master page) you will get an error from server.

    Its a way of distinguishing the files that can`t be browsed as stand alone pages, in Razor view engine.

    Think of it this way... in MVC 2 ... you would differentiate the partial view and the mastersite with the sufix .master, .ascx, and normal pages are .aspx, on the other hand, in Razor view... all views are .cshtml, so to distinguish partial and masterpages they will have a prefix (_). its nothing mandatory, just a "convention".

提交回复
热议问题