Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a razor page in Blazor Project creates a .cshtml file

I'm following a tutorial trying to create a blazor project.

It says to add a razor page. When I right click on Pages folder ->Add->Razor Page, I get this prompt.

Prompt to create razor page

No matter what options I pick, the new file is a .cshtml file, not a .razor file.

I see other .razor files in the same folder by default.

Target Framework: .NET Core 3.1

Am I missing something?

like image 384
vbp13 Avatar asked Nov 05 '19 19:11

vbp13


People also ask

Can you mix MVC and Razor pages?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.

What is the difference between Blazor and Razor pages?

Razor is a templating engine that combines C# with HTML to build dynamic web content. Blazor is a component-based, single-page app framework for building client-side web apps using . NET that works well with all modern browsers via WebAssembly for client-side Blazor.

How do I create a cshtml page in Blazor?

Select “ASP.NET Core” from the left panel, then select “Razor Page” from the templates panel and name it “EmployeeData.cshtml”. Click “Add”. This will add an “EmployeeData.cshtml” page to our “BlazorSPA.Client/Pages” folder.

How to render Blazor components from an Existing razor page?

We start with an existing Razor Pages application which has been converted to .NET Core 3: First, you have to add Blazor support into your application. This support will allow you to render Blazor components from a Razor page. The official documentation goes through the process but here’s a quick rundown of it.

What are the files in a Blazor project?

Most files in Blazor projects are.razor files. Razor is a templating language based on HTML and C# that is used to dynamically generate web UI. The.razor files define components that make up the UI of the app. For the most part, the components are identical for both the Blazor Server and Blazor WebAssembly apps.

How do I create a razor page in Visual Studio?

Right-click Counter.razor and delete it. Right-click the Pages folder. Select Add -> Razor Page and create a new page named Counter.razor. If you receive a “scaffolding” error, you can instead select another file format and give it a .razor file extension. A Razor page is a self-contained view.


1 Answers

I'm using Microsoft Visual Studio Professional 2019, Version 16.4.3.

Instead of creating a new "Blazor Page", go to the dialog to add a "New Item". From ASP.NET Core, Select "Blazor Component". This will create a new file called YourComponent.razor.

Bonus Fact: To create a code-behind file for the component, add a new class and specify the name YourComponent.razor.cs. Declare the class partial (because the .razor file itself makes a partial class declaration).

I am also new to Blazor and this has been a source of confusion for me. I hope that they make it more straight-forward in the next version of Visual Studio.

like image 154
Vivian River Avatar answered Jan 01 '23 19:01

Vivian River