Chuck Bevitt
Chuck Bevitt

Reputation: 565

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:

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#.

Upvotes: 6

Views: 2284

Answers (1)

Artak
Artak

Reputation: 2887

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.

Upvotes: 6

Related Questions