Different types of Razor files

All Razor files end with .cshtml. Most Razor files are intended to be browsable and contain a mixture of client-side and server-side code, which, when processed, results in HTML being sent to the browser. These pages are usually referred to as "content pages". Other Razor files have a leading underscore (_) in their file name. These files are not intended to be browsable. The leading underscore is often used for naming partial pages, but three files named in this way have a particular function within a Razor Pages application.


The _Layout.cshtml file acts a template for all content pages that reference it. Consistent part of a site's design are declared in the _Layout.cshtml file. These can include the header, footer, site navigation and so on. Typically, the _Layout.cshtml file also includes the <head> section of the page, so they also reference the common CSS style sheet files and JavaScript files including analytics srvice's files. If you want to make changes to the overall design of the site, you often only need to make adjustments to the content of the _Layout.cshtml file.


The _ViewStart.cshtml file contains code that executes after the code in any content page in the same folder or any child folders. It provides a convenient location to specify the layout file for all content pages that are affected by it, and that is typically what you see in the _ViewStart.cshtml file that comes with any Razor Pages (or MVC) template.


The purpose of the _ViewImports.cshtml file is to provide a mechanism to make directives available to Razor pages globally so that you don't have to add them to pages individually.

The default Razor Pages template includes a _ViewImports.cshtml file in the Pages folder - the root folder for Razor pages. All Razor pages in the folder heirarchy will be affected by the directives set in the _ViewImports.cshtml file.

Last updated: 08/05/2019 14:29:06

© 2018 - 2024 - Mike Brind.
All rights reserved.
Contact me at Outlook.com