Very simple Blazor calls very simple API web app

David Thielen 3,121 Reputation points
2024-09-12T18:05:17.4+00:00

Hi all;

This is three associated questions. I need to create a simple web app that has a single controller. It is passed 3 parameters and returns an array of strings. What should I create in Visual Studio?

I then need to have a simple Blazor server app that had 2 edit boxes. When those are filled and the submit button clicked, it calls the API web app, gets back the asrray of ints, and displays them.

I know how to create the sample Blazor server app (counter and temperature). But all my forms use DevExpress. Is there a sample somewhere about how to create a simple form in Blazor using native edit boxes?

And finally, any guidance in the Blazor app adding a GUID the the request header and the API app reading it. This will be the key used for authentication.

I'm asking the above because I'm finding lots of conflicting ways to accomplish the above. If I'm going to learn something new, I want to learn the best way to do it.

thanks - dave

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,573 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,584 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 65,576 Reputation points
    2024-09-12T20:12:35.73+00:00

    for a single controller api, I'd just create a webapi minimal

    % dotnet new webapi -minimal

    to get the header:

    app.MapPost("api/doit", async (
       [FromForm] string p1,
       [FromForm] string p2,
       [FromForm] string p3,
       [FromHeader(Name = "xx-name")] string headerValue ) =>
    {
        var list = await CreateList(...);
        return Results.Ok(list);
    });
    

    if you end up with multiple api calls, I use middleware to parse the custom header and update an injected object;

    for HttpClient see this thread:

    https://stackoverflow.com/questions/35907642/custom-header-to-httpclient-request

    and finally if its just a one page app with a simple form. I'd use razor pages instead of Blazor

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.