I am new to C# & Blazor and I appreciate your help!
I have started two new projects, one Server Project (.NET 5.0) and one Blazor WebAssembly Client project. The problem is that I am trying to GET json data from the Server, but I am receiving the exception:
Unexpected character encountered while parsing value: <. Path '', line 0, position 0.).
After debugging, I find that the Content of my returned HttpResponseMessage (converted to string) contains the index.html of the client project. More specifically, the variable "responseString" of the method "Deserialize" inside the "HttpService.cs" (shown below) contains the index.html of the client project instead the json data from the server.
The server sends the data properly. I have confirmed it with swagger.
The method in my Blazor WebAssembly App for getting data:
public class ContractsServices : IContractsServices
{
private readonly IHttpService _httpService;
private readonly string url = "api/Contracts";
public ContractsUIRepository(IHttpService httpService)
{
_httpService = httpService;
}
public async Task<List<ContractDTO>> GetAllContracts()
{
var response = await _httpService.Get<List<ContractDTO>>(url);
if (!response.IsSucceed)
{
throw new ApplicationException(await response.GetBodyOfResponse());
}
return response.Response;
}
}
My HttpService.cs:
public class HttpService : IHttpService
{
private readonly HttpClient _httpClient;
public HttpService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<HttpResponseWrapper<T>> Get<T>(string url)
{
var httpResponseMessage = await _httpClient.GetAsync(url);
if (httpResponseMessage.IsSuccessStatusCode)
{
var response = await Deserialize<T>(httpResponseMessage);
return new HttpResponseWrapper<T>(true, response, httpResponseMessage);
}
else
{
return new HttpResponseWrapper<T>(false, default, httpResponseMessage);
}
}
private static async Task<T> Deserialize<T>(HttpResponseMessage httpResponseMessage)
{
var responseString = await httpResponseMessage.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(responseString);
}
}
The HttpResponseWrapper.cs:
public class HttpResponseWrapper<T>
{
public bool IsSucceed { get; set; }
public T Response { get; set; }
public HttpResponseMessage HttpResponseMessage { get; set; }
public HttpResponseWrapper(bool isSucceed, T response, HttpResponseMessage httpResponseMessage)
{
IsSucceed = isSucceed;
Response = response;
HttpResponseMessage = httpResponseMessage;
}
public async Task<string> GetBodyOfResponse()
{
return await HttpResponseMessage.Content.ReadAsStringAsync();
}
}
My Program.cs in the client project:
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped<IHttpService, HttpService>();
builder.Services.AddScoped<IContractsServices, ContractsServices>();
await builder.Build().RunAsync();
}
}
Thank you in advance for your time and your help!!!