Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "razor" and "cshtml" files?

Tags:

asp.net

razor

What is the difference between "razor" and "cshtml" files in ASP.NET. Should we use ".razor" file in "razor-components" application instead of ".cshtml"?

like image 292
BALA MURUGAN Avatar asked Apr 09 '19 10:04

BALA MURUGAN


People also ask

What are Cshtml files?

What is a CSHTML file? A file with . cshtml extension is a C# HTML file that is used at server side by Razor Markup engine to render the webpage files to user's browser.

Can I use Razor components in Cshtml?

cshtml works within a Razor Pages application except that it only affects Blazor artefacts. It provides a way to bring namespaces into scope within Razor components placed in the same folder as the file, or any sub folders. The code at the top of the file is familiar to anyone who has worked with Razor.

What's the difference between Cshtml and HTML?

html is strictly processed by the client, typically a browser. cshtml is the file extension that refers to the razor view engine. In addition to straight html, these files also contain C# code that is compiled on the server prior to the pages being server up to the browser..

Which is better Razor or MVC?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.


2 Answers

It depends on which ASP.NET you mean...

For example, in terms of Blazor:

As of .NET Core 3.0 Preview 4 SDK (3.0.100-preview4-011223) it is noted:

  • Rename all _ViewImports.cshtml files to _Imports.razor.
  • Rename all remaining .cshtml files to .razor.

So if you are building a Blazor Web App using .NET Core 3.0 Preview 4 and later, you should be using .razor instead of .cshtml.

Source: https://devblogs.microsoft.com/aspnet/blazor-now-in-official-preview/

like image 174
PiotrG Avatar answered Oct 02 '22 06:10

PiotrG


Razor is a markup syntax that lets you embed server-based code into web pages using C# and cshtml is the extension of razor file Cshtml = cs (C#) + HTML

Components are typically implemented in Razor Component files (.razor) using a combination of C# and HTML markup (.cshtml files are used in Blazor apps).

.cshtml and .razor is the same thing we use .cshtml file in blazor app and .razor in razor components

like image 43
Alaaeddine HFIDHI Avatar answered Oct 02 '22 06:10

Alaaeddine HFIDHI