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

asp.netasp.net Mvc-3Razor

asp.net Problem Overview


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?

asp.net Solutions


Solution 1 - asp.net

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 - asp.net

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.

Solution 3 - asp.net

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

Solution 4 - asp.net

As far as I know this is simply a convention used to identify the intent of the file; I don't believe it will actually change the behavior of the file. In most development contexts, prepending an underscore identifies something to be meant for "private" use, whether by a class, or in this case, another template.

Solution 5 - asp.net

I dont use MVC, but with web pages which also uses the razor syntax, the _ prefix generally siginifies that the page is not meant to be accesssed by a user but by other pages or some code. If you try to navigate to a page that contains the _prefix, asp.net would prevent access to it. Thats why its used with layout pages and other such pages since they should not be accessed directly by a user.

Something like the App_Code folder in asp.net

Solution 6 - asp.net

Right-click on the Index.cshtml file and select View In Browser. From this, we can test the index.html page in the browser(with out running the app).

Do the same for _Layout.cshtml page, it will show you error or browser will render the default page(Home/Index.cshtml).

Because the pages prefixed with _ will not be tested through browser.

We can test those pages(_Layout. cshtml) by embedding with another cshtml pages.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrichbView Question on Stackoverflow
Solution 1 - asp.netMike BrindView Answer on Stackoverflow
Solution 2 - asp.netMichael StumView Answer on Stackoverflow
Solution 3 - asp.netJuztinView Answer on Stackoverflow
Solution 4 - asp.netfuturealView Answer on Stackoverflow
Solution 5 - asp.netTravisView Answer on Stackoverflow
Solution 6 - asp.netCoCoView Answer on Stackoverflow