PepperTiger
PepperTiger

Reputation: 604

Configure route in a Blazor Server-Side client project

I'm making a Blazor server-side SPA client app which communicate with API controllers on a MVC server through HTTP requests.

What should I add in both Startup.cs of the client and server to make the routing work when making the HTTP request ? And how can I configure the routes ?

Upvotes: 0

Views: 1904

Answers (1)

enet
enet

Reputation: 45626

OK... The BlazingPizza sample is a Blazor WebAssembly application. It is executed on the client browser. The solution contains three projects: Shared (shared by the other two projects); Client ( executed on the client browser); and Server (a project which host the application and the Web Api). Now, you want to convert this application to Blazor Server app, right ?

Do this and you're safe: 1. Go to Visual Studio and create Blazor Server App (from the Create a new Blazor app dialog). After hitting the "Create" button, a new Server-side Blazor application is created and configured for you. Run this app and see what it can do, view the Startup class to learn how your app was configured, etc.

Realize this: This is an Asp.Net Core app, running on the server, and sending Html content via SignaleR to connected clients. It is important that you realize that this (the Html you see in the browser) is the client-side of the Blazor Server app, but execution of code occurs on the server only,and thus, if you use a Web Api app to your application, it is executed on the server; in other words, from the server you make Web Api calls to the server. This is legitimate, and sometimes even necessary...

  1. To add a Web Api project to your application do the following: Right click on the solution name, select Add --> New Project... From the Add a new project window select Asp.Net Core Web Application. From the Create a new Asp.Net Core web application window select the API project template, and then hit the "Create" button. A new Web Api project is created and configured for you with a controller named WeatherForecastController.

  2. You are going to call the Web Api endpoint from your Blazor Server app, right ? So add a reference to the Web Api project from the Blazor Server app. Now you can perform HTTP calls from your Blazor Server to the Web Api endpoints.

What I've described above is how you do what you've asked in the comment above. But this is only the beginning. Start to play with it, and please, if you have more questions, open new question threads, and I'll try to help...

Upvotes: 3

Related Questions