3HMonkey
3HMonkey

Reputation: 137

How to use cshtml (Razor Pages) in a Blazor-Server-App

well I am pretty new to Blazor. I have been working with NodeJS alot before. For a new use case I wanted to know more about Blazor, so I am currently investigating in that. Well, what am I planning to do? I'd like to test the components that deal with the entity framework including CRUD-operations. For this I had done a few tests in the past. I personally like that process (it is very inovative and effective) to automatically generate "Razor Pages" for CRUD-operations due to a data model and a data provider. Baaaam, it creates you fully working pages like "edit", "delete", "update" and so on. But thats where the problem came up... In terms of content, these pages are perfect. However, they are rendered without any layout or functionality (maybe interpreted wrong?). So I decided to build up a clean and fresh project.

  1. Created a server side blazor app
  2. Created a folder "Test" inside the "Pages" folder
  3. I created a "Razor Component" with some demo content
  4. enter image description here

  5. Same for "Razor Page" what I like to do because I like to split code and markup + I need it for generating that CRUD stuff later on. But for now, here is just the demo content.

  6. enter image description here

  7. List item

  8. Finally the rendered part. As you can see, if I access the Test/index (*.razor) everything is rendered fine like menu, layout and as a single page appplication.

  9. List item

  10. And finally here is the output of the "Razor Page" (*.cshtml). Content is just rendered without layout, without CSS, without hyperlink interpretation and so on.

  11. List item

    Do you know what am I doing wrong? I really like to work with these page gernerators. It is amazing and efficient. Maybe you guys have some tips for me.

Thank you really much.

Greetings

Upvotes: 8

Views: 11229

Answers (2)

Alexander Gräf
Alexander Gräf

Reputation: 576

You can absolutely run classic Razor pages inside a Blazor app, even a client-side one. In the end, there is a pipeline, and given a URI, that decides what content to serve to the client.

However, there are plenty of drawbacks. For example, if you are using any sort of Blazor component library, you have to jump through hoops to manually render these in your Razor pages.

Since this question is quite old, and modern Blazor implementation allows hybrid-rendering and arbitrary splitting between server and client (called "interactive Server and interactive client rendering"), there isn't much reason to host Razor pages anymore, because as I wrote, without manual rendering, it's quite difficult to make them look identical to the rest, and/or re-use Blazor components in general.

Upvotes: 2

enet
enet

Reputation: 45596

How to use cshtml (Razor Pages) in a Blazor-Server-App

You can't host a Razor Pages App in a Blazor App. But you can host Blazor components in a Razor Pages App. If you host the App Component of a Blazor app, you actually host the whole of the Blazor app in your Razor Pages app.

Upvotes: 8

Related Questions