156

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?

1
  • I use NancyFX with Razor and since by default it restricts any content that's not in the Content folder. (This can be overridden in web.config or custom config) it's impossible to serve any files at all such as .cshtml directly. So I don't use "_" prepended to my view names because it's NOT necessary and ugly. Commented May 19, 2016 at 9:35

6 Answers 6

220

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.

11
  • 6
    Thanks. For me this is the most insightful answer. I was under the misapprehension that Razor was tied to MVC. Now I see the reason for the leading underscore is to prevent them from being served directly under ASP.NET Web Pages.
    – richb
    Commented Jan 3, 2011 at 1:53
  • 1
    A naming convention that actually has a functionality tied in, I thought MS would know better. And now it's carried over to MVC, which was supposed to be a clean slate.
    – Boris B.
    Commented May 28, 2012 at 12:39
  • Hopefully after the current .NET Framework 4.5.1 and Visual Studio 2013 releases including "One ASP.NET" functionality they can finally move away from these technical limitations/hard-coding. Of course having standard files never shared is essential as with current *.config, APP_Code and APP_Data directories. But this logic should sit in a config file somewhere (machine config as default) so it can be overridden. Also these default names of common pages should be configurable (Layout/Error/etc...).
    – Tony Wall
    Commented Feb 7, 2014 at 11:05
  • 1
    @Daniel Oh, I see what you mean. I have edited the answer because it doesn't work as advertised.
    – Mike Brind
    Commented Oct 17, 2018 at 7:35
  • 1
    @Daniel What you are seeing is apparently a bug that surfaced in Razor Pages 2.1. It is scheduled to be fixed in 2.2. It works as I described in 2.0.
    – Mike Brind
    Commented Oct 18, 2018 at 14:45
12

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.

2
  • this is incorrect, as pointed out by the answer above - underscore has a security functionality.
    – iJungleBoy
    Commented Jul 2, 2017 at 11:40
  • 1
    @iJungleBoy See the accepted answer. for ASP.net MVC (which this question is about), there is no security functionality. See the web.config in the Views folder that already blocks all cshtml and aspx files, underscore or not (sets up System.Web.HttpNotFoundHandler for them). Commented Jul 2, 2017 at 21:58
9

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

7
  • 4
    But by that logic wouldn't ALL cs & cshtml files be prefixed with an underscore?
    – richb
    Commented Jan 2, 2011 at 1:01
  • if all files would have _ as a prefix then your site wouldnt work... files that have _prefix are rendered inside a normal page...(for partials), and the sitemaster is a template... so it must have content to be displayed.
    – Juztin
    Commented Jan 2, 2011 at 1:05
  • So I just tried this, and IIS on my box doesn't serve any files from the Views, directory. Not even static .html files. So I really don't think this is the answer.
    – richb
    Commented Jan 2, 2011 at 1:08
  • Juztin: The question is why do they begin with an underscore? If I rename _Layout.cshtm to Layout.cshtml it still works just fine. So what is the reason for this convention?
    – richb
    Commented Jan 2, 2011 at 1:11
  • 2
    The question is about asp.net mvc, not webpages
    – fabspro
    Commented Sep 27, 2012 at 15:29
2

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.

2

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.

1

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

3
  • @MikeBrind You can't 'navigate' to/browse directly any of the views under /view in a default ASP.NET MVC project; the /views/web.config file is set up to prevent it. But there's nothing to prevent a controller action from returning View("_Index", model); It works just fine; I just did it by changing a view's name to _Index.cshtml and changing the action to call as I did above. Commented Nov 26, 2012 at 13:03
  • @MikeBrind This question is about MVC, not Web Pages. Granted; I did not specify that in my original comment. Commented Nov 27, 2012 at 18:16
  • @MikeBrind I was - and am - responding to this answer. Not yours. My initial comment was misleading (I did also mention "partials", though), so I have deleted it. My point was and is that underscores have nothing to do with not being able to load a view in MVC. This user even started saying, "I don't use MVC", yet this question was about MVC. I'm just making sure someone coming along later reading this answer doesn't somehow think that in MVC, an underscore affects the ability of a controller action to load a view. No big deal. We agree I was inartful in how I spoke. Done. Commented Nov 27, 2012 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.