I need help to replicate how I consume my API that currently I have in ASP.NET MVC in Blazor Webasse

New Contributor

I'm new to Blazor Webassembly App currently working with ASP.NET MVC and I need help to replicate how I consume my ASP.NET API in Blazor Webassembly App.

I am going to leave how I have configured it in my current project so that you know how I have everything in ASP.NET and I would like to know how I have to do the same in Blazor Webassembly App.

 

In my current project I have a JSON file that I have created with the AuthUrl and the ClientID:

 

 

<appSettings>
  <!-- Application Connection Settings -->

  <add key="URL_Resource" value="https://xxxxxx/v1/api/" />

  <!-- Application Client ID to direct to HIS -->
  <add key="ClientId" value="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX" />

</appSettings>

 

 

Then I have a controller called BaseController where I pass it through Json parameters.

 

 

 public abstract class BaseController : Controller
    {

        private IResource _Resource;
       
        {
            get
            {
                var appKey = ConfigurationManager.AppSettings["ClientId"];
                if (_Resource== null)
                    _Resource= new EurekaResource(ConfigurationManager.AppSettings["URL_Resource"], appKey);

                return _Resource;
            }
        }

 

 

 

Finally I have a class called IResource where I have my endpoints.

 

 

  public class IResource
    {
        private readonly string _baseUrl;
        private readonly string _appkey;


        public IResource(string baseUrl, string appkey)
        {
            if (string.IsNullOrWhiteSpace(baseUrl)) throw new ArgumentException("BaseUrl cannot be null or empty.");

            if (appkey == null) throw new ArgumentNullException("The appkey argument cannot be null.");

            _baseUrl = baseUrl;
            _appkey = appkey;
        }

       
        public async Task<Patient> GetPatientByIdAsync(long patientId)
        {
            try
            {
                return await Get<Patient>(string.Format("patient/{0}", patientId.ToString())).ConfigureAwait(false);
            }
            catch (Exception)
            {

                throw;
            }            
        }

 

 

 

My models is a DTO.dll file where I add it to the project in the dependencies.

 

Is someone can Guide me how to do the same in Blazor would be great!

2 Replies
To clarify a bit this post is exactly what I use now in ASP.NET MVC and it works fine for me I am a bit confused how to do it in Blazor basically I want to consume an API that has a URL and Client ID and it is difficult to get an example I saw many examples from local host other people add and register in Program.cs so I would like if someone can guide me how to do it what is the correct way I don't know if I have to set something in appsettings.json or create a controller just need this one guide at least to know correctly how to do it.

Hi @gebernal89,

Thanks for posting your issue here.

However this platform is used for how-to discussions and sharing best practices for building any app with .NET.Since your issue is a technical question, welcome to post it in Microsoft Q&A forum, the support team and communities on Microsoft Q&A will help you for any technical questions.
Besides, it will be appreciated if you can share it here once you post this technical question Microsoft Q&A.
Best Regards,
Lan Huang