Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to mix client side and server side methods in Blazor?

As an example, suppose I'm creating a simple Change Password page:

  • User Name
  • Current Password
  • New Password
  • Confirm New Password

Over all, I would want to use server side Blazor to eliminate the need to write a separate API back end. However, I would want some simple validation code to run in the client - New and Confirm passwords don't match, password complexity, etc. Can some Blazor methods run in the client, in a server side Blazor app? Or even better, can the same validation methods run BOTH client side and server side?

Of course, I could always fall back to JavaScript for client side validation, but it'd be nice to code everything in C#.

like image 814
Chuck Bevitt Avatar asked Jan 08 '20 22:01

Chuck Bevitt


1 Answers

Short answer is "no".

However, the closest to what you're looking for is the Blazor WebAssembly Hosted project template, which is Blazor WASM with a backing ASP.NET Core web application hosting it. Your client side code will be in C# and you will be able to run whatever validation you're looking for. However, you will have to explicitly call an an API exposed in the hosting web application (backend), using an instance of HttpClient. That way you have full control over what code runs on client and what on server.

The default hosted project template has a great example of this (the fetch-data page). You can create it using the dotnet new blazorwasm --hosted and see how it's done there. Hope this helps.

like image 115
Artak Avatar answered Oct 13 '22 20:10

Artak