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

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

  • _viewstart
  • _Layout
  • _LogOnPartial

Why this convention, and what is this used for? Do I need to follow this convention?

Does the framework give some special meaning to a .cshtml file that begins with an underscore?


Solution 1:

Razor was developed for ASP.NET Web Pages (WebMatrix), which doesn't have the same sort of protection built in regarding Views folders and Routing that you get within MVC. 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. Other .cshtml files within Web Pages generally need to be browsable. They are the equivalent of .asp or .php files.

The ASP.NET team have stated that Web Pages is a starting point within ASP.NET development, which should lead to migration to MVC in time (for those that want to move on). Part of that means that it should be as easy as possible to migrate from Web Pages to MVC. Consequently, it makes sense to carry over naming conventions established within Web Pages to MVC Razor files.

So there is a technical reason for prefixing the file names with an underscore - it just isn't relevant to MVC.

[UPDATE Oct 2018]

In the new ASP.NET Core Razor Pages framework (apart from in version 2.1), files with a leading underscore are ignored when routes are being generated at startup - even if they have an @page directive (which would normally make them a routeable Razor Page). That's why it makes sense to name layout and partial files with a leading underscore in a Razor Pages application if they are not intended to be browsed.

Solution 2:

That's how Ruby on Rails does it (Partials start with a _ but the Render Partial call does not include the _), and ASP.net MVC has drawn heavy inspiration from it.

No technical reason really, just a convention to clearly show the intent to other developers (and yourself 6 months later) to say: This is a partial view.