Blazor Server and Client app, Azure question
I have Blazor Server and Client app. I deployed it on Azure Web App (not static).
When I open the created buy Azure link, client works, however I client uses wrong route for post method. How can I get the route where my Blazor Server is listening to requests?
35 Replies
are you using an
HttpClient
to post?Yes
I think you can just set the
HttpClient
base address. https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.baseaddress?view=net-8.0HttpClient.BaseAddress Property (System.Net.Http)
Gets or sets the base address of Uniform Resource Identifier (URI) of the Internet resource used when sending requests.
well, didn't help
System.InvalidOperationException: net_http_client_invalid_requesturi
blazor.webassembly.js:1 at System.Net.Http.HttpClient.PrepareRequestMessage(HttpRequestMessage )
var response = await _httpClient.PostAsync(_httpClient.BaseAddress+"Blob", content);
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
if you set base address, everything else is relative
so
var response = await _httpClient.PostAsync(_httpClient.BaseAddress+"Blob", content);
is wrong
should be
var response = await _httpClient.PostAsync("Blob", content);
what is a good resource to read about that?
because when I read documentation, except basic things, I feel myself stupid
And still doesn't work.
Event input checks from Data Annotations do not work. Did I deploy it incorrectly?
should I provide any code or links?
I assume the URI is different for the server and client, is the BaseAddress set correctly?
In the remarks
That specifies the request behavior if you use a relative Uri
I did not touch it actually
It's hard to know exactly, it probably depends on your deployment. When you say server and client, I assumed two different web app deployments
One web app deployment
Just asp blazor template consists of Server blazor proj and Client blazor proj
So the server proj was deployed. But it calls the Client when I run it.
by blazor client, you mean blazor wasm?
It was a Blazor WebAssembly template
yep
ahhh, hmm...I see, I was thinking slightly different
For wasm I think you just always use relative paths and it "just works"
unless your wasm base path isn't the root
to clarify
It perfectly works during debugging
But for me Azure is as new as Blazor. And I can't figure out where is my problem.
How are you deploying? That probably has something to do with it.
Deployed to Web App. Deployed Server app (like from the screen above)
Is it always so painful to deploy to Azure?
idk tbh, I have only done small projects. I would have assumed you had a webapi project instead of blazor server, since that has it's own page rendering. Is the wasm package deployed from your blazor server app?
or is it hosted as a static app? like https://learn.microsoft.com/en-us/azure/static-web-apps/deploy-blazor
Deploy a Blazor app on Azure Static Web Apps
Learn to deploy a Blazor app on Azure Static Web Apps.
I created Web App, not static one. So it should be wasm
My project is as small as possible. Just this deploying thing..
I have looked though so many articles. It should be much easier. But it just doesn't work
Yea hang on, I'm trying this on my end to fully understand what you're doing. It seems odd to me that you're deploying both the WASM and SSR (server side rendering) to the same web app
I hope I didn't mislead you
Anyway, I tried to deploy separately.
Client to Static Web App.
Server to Web App.
Client was working. But not Server.
And even here, I couldn't see where was my problem exactly
what's not working on the server?
this is what I get with the base template + deploy to azure web app without changing anything https://ryzngardtest.azurewebsites.net/
for example: my controller action takes a model as a parameter. Data annotation is used for validating this model. Deployed app does not validate user input.
And a post request from Client to Server does not work as well
Did you use the same sample project as I did?
ah, I just did
dotnet new blazorserver
I didn't see you were using MVCThat is probably what I should have done. But I wanted to have at least some architecture in my project. Which is total mess now
It is not a live/death question, you don't have to waste so much time.
I though somebody would come up with an easy solution
eh, I'm just doing code reviews right now and answering in between
I would suggest web api for server, and wasm for client
or just do a new blazorserver and have it all server side
mvc isn't blazor, and the naming will always haunt us. mvc is asp.net. I'm asuming this is the template you did in VS?
I started like that. But with Blazor Server...
Doesn't matter.
What would be different if I did like you said?
Different in terms of deploying process
it should be the same, so I'm a little confused with you as to why what you're doing isn't working
but with web api you're doing what you should: making endpoints only, not doing page rendering. All UI would be blazor wasm
that's also where you would do form validation etc, in the WASM side
I don't really understand the use of two UI based projects
it can be done, but you're conflating ideas and usage, and that's probably causing a lot of different advice to be wrong
(including my own 😦 )
Yeah, I wish I could go back a few days.
What about routing from wasm to api?
That is what I was afraid of
do you mean how do you set the URL of http requests? That would be the base url on the HttpClient like I said earlier. You can use specific environment app configs to make that easy
After deploying my routes usually go away..
Thank you for your time, Andrew
Have a good day or night
good luck 🙂 I'll be around if you're still hitting issues. Try webapi instead of asp.net mvc