What is the difference between "razor" and "cshtml" files in ASP.NET. Should we use ".razor" file in "razor-components" application instead of ".cshtml"?
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:
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/
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
razor helps you embed serverside code like C# code into web pages. cshtml is just a file extension. razor view engine is used to convert razor pages(.cshtml) to html.
.cshtml files are razorpages or MVC views, they does not contain any C#-written client-side code. If you wan to do so, you must use JavaScript. However, a .razor file, also know as a Razor component, can have C# written in it and run on client's browser.
Used in your standard .cshtml files, and implemented like so:
<BlazorSample.Components.MyComponent />
These new extensions are used for Razor Components.
https://docs.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-3.1
I might be wrong but .cshtml file created when adding a new Razor page is a new html page, and .razor file created when adding a new Razor component is a component that will fit into a Razor page.
Since Blazor can execute C# on Client side, now there are 2 places - client and server - where Razor code can be executed. So client side has extension ".razor" and server side - ".cshtml"
I created a new WebAssemblyHosted application and found that: