Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining MVC + Blazor in the same project

Our current application is now running on ASP.NET Core (MVC) and I was wondering is there will be an offical way to use MVC and Blazor (client side) in the same project?

The reason why I want to do that is because we won't be able to migrated from MVC to Blazor in one big bang since, the application is just too big. I was thinking on a step by step transition from MVC to Blazor. Just not sure if this will be possible?

like image 216
gsharp Avatar asked Feb 11 '19 12:02

gsharp


People also ask

Can you mix Blazor and MVC?

Blazor applications are component-based. Blazor components can be used in existing ASP.NET MVC applications.

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.

Can you mix Blazor and Razor?

Razor components can be integrated into Razor Pages and MVC apps in a hosted Blazor WebAssembly solution. When the page or view is rendered, components can be prerendered at the same time.


2 Answers

This is definitely now possible for server-side Blazor, Chris Sainty's blog (and the source for the example in that blog) gives an example of exactly how to do that.

Personally I prefer to have the Blazor components in a separate project in the same solution as the MVC project. There are a few reasons for that:

  • I find Blazor projects are faster to start up than MVC
  • I've only managed to make the automatic rebuilding of the project (basic hot reload) work in a Blazor project, not in an MVC project
  • I've found it useful to be able to isolate the Blazor components to be sure that issues aren't related to the MVC parts of a page.

I tend to do the initial debugging with the Blazor project set as the startup project and then switch to the MVC project when I'm ready to integrate the component into an MVC page.

If you want to try that approach, my answer to the question Adding Server-Side Blazor to an existing MVC Core app gives a complete walk through of how to add a separate Blazor project to an existing solution that contains an MVC project, and then how to use those Blazor components in the MVC project.

I expect very similar approaches would apply for client-side Blazor too, but I haven't tried that yet.

like image 200
tomRedox Avatar answered Oct 12 '22 19:10

tomRedox


According to ASP.NET Core 3.0 Preview 2 release notes

Currently you cannot use Blazor directly with ASP.NET Core MVC but you can use with newly introduced Razor Component. Microsoft is expecting using Blazor directly with ASP.NET Core MVC will be possible in the upcoming versions of ASP.MET Core.

For more details: ASP.NET Core updates in .NET Core 3.0 Preview 2

like image 24
TanvirArjel Avatar answered Oct 12 '22 18:10

TanvirArjel