What is the CSHTML file format?
A .cshtml file is an ASP.NET Razor server-side web template that combines C# code and HTML markup in a single source file. The format was introduced in January 2011 with ASP.NET MVC 3 as part of Microsoft's Razor view engine.
The @ character is the core syntactic marker: wherever it appears, the Razor parser switches from HTML rendering mode into C# execution mode. A view may include @model declarations, @using namespace imports, @functions blocks, tag helpers, HTML helpers, and @section slots for a shared _Layout.cshtml page. The Razor engine compiles the file server-side to C#, then to .NET intermediate language (IL); only the rendered HTML is ever sent to the browser - the .cshtml source is never exposed to clients.
The cs prefix distinguishes these files from .vbhtml Razor views that use Visual Basic instead of C#. ASP.NET Core 2.0 introduced Razor Pages, which also use .cshtml but add an @page directive at the top, making the file act as its own self-contained controller and view. For single-page application patterns, Blazor components use the .razor extension and largely replace .cshtml in new ASP.NET Core projects.
.cshtml files are edited in Visual Studio or Visual Studio Code with the C# Dev Kit. Opening one directly in a browser displays raw source code rather than rendered HTML.
Security & safety
RISK: LOW.cshtml is a plain text source file - opening it in a text editor or IDE is harmless. Servers should be configured to block direct HTTP access to .cshtml files (IIS and Kestrel do this by default). A misconfigured server that serves .cshtml as raw text could leak application logic and connection strings - a security risk for the server operator, not for the person reading the file locally.
Format details
in a nutshellPrograms that open CSHTML files
Technical details
deep spec| Encoding | UTF-8 text (default in Visual Studio; other encodings supported) |
| Container | Plain text - interleaved C# code and HTML markup; no binary wrapper |
| MIME type | text/html (rendered output to browser); text/plain for the source file in editors |
| Razor syntax marker | @ character switches from HTML rendering mode into C# execution mode |
| Compilation | Compiled server-side by the Razor engine to C#, then to .NET IL; source file never delivered to the browser |
| Key directives | @model, @using, @functions, @section, @page (Razor Pages), @inject |
| Razor syntax version | v1 (MVC 3, 2011) → v2 (MVC 4) → v3 (MVC 5/Core 1) → Razor 8 (ASP.NET Core 8, 2023) |
| Tag helpers | Supported from ASP.NET Core 1.0; replace HTML helpers for server-rendered attribute generation |
| Layout system | _Layout.cshtml defines shared page chrome; views use @section blocks to inject content |
| Associated OS | Windows (IIS/IIS Express), Linux and macOS (Kestrel, Docker) |
| Razor Pages variant | @page directive makes a .cshtml file a self-contained controller+view (ASP.NET Core 2.0+) |
| Client exposure | Source file never served to the browser; direct access returns raw text or an HTTP error |
| VB counterpart | .vbhtml (identical Razor syntax using Visual Basic instead of C#) |
| Modern successor pattern | .razor (Blazor components) for SPA-style applications in ASP.NET Core |
| Released | 2011 (ASP.NET MVC 3 / Razor syntax v1) |
| Latest version | Razor 8 (ASP.NET Core 8.0, November 2023) |
| Specification | learn.microsoft.com |
CSHTML conversions
Community Q&A
asked by usersNo questions yet - be the first to ask about CSHTML files.