I'm working on a Blazor WASM project, and I have a page that needs to post a file to the server. So I wrote something like this:
@page "/Test"
@inject HttpClient httpClient
<h4>Please upload a file.</h4>
<form>
<InputFile OnChange="OnInputFileChange"/>
</form>
@code {
private async Task OnInputFileChange(InputFileChangeEventArgs e)
{
// Returns 1 just fine
var dummyTest = await httpClient.GetFromJsonAsync<int>("api/Test/Action");
// Gets a 404 error
var response = await httpClient.PostAsJsonAsync("api/Test/Action", e.File);
var content = await response.Content.ReadFromJsonAsync<int>();
}
}
and I have this controller
[ApiController]
[Route("api/[controller]/[action]")]
public class TestController : ControllerBase
{
[HttpGet]
public async Task<ActionResult<int>> Action()
{
return 1;
}
[HttpPost]
public async Task<ActionResult<int>> Action(IFormFile file)
{
return 1;
}
}
My issue is that the GET request executes just fine and dummyTest is set to 1, but response gets a 404 and trying to call ReadFromJsonAsync gives System.Text.Json.JsonException: The input does not contain any JSON tokens..
I'm not sure what I'm missing here.
Thanks in advance!
e.Fileis anobjectand you're expecting an interface. You almost certainly need to stick to passing concrete classes. Caste.Filetomyfileclassin your caller method and expectmyfileclassin your controller.