I created a standard Blazor Server app. Then I added an API Controller with read/write actions.
Now I want to call an action from the index page, but it doesn't work. The app runs without error, but doesn't return the expected (status = "Waiting for activation", Method = "null" and result = "Not yet computed"). I put a breakpoint in the controller action, but the program never hits it.
Controller:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET: api/<ValuesController>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET: api/<ValuesController>/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
}
Index page:
<button class="btn btn-primary" @onclick="RetrieveGet">
GET
</button>
void RetrieveGet()
{
HttpClient Http = new HttpClient();
string baseUrl = "https://localhost:44382/";
var temp2 = Task.Run(async () => { await Http.GetStringAsync($"{baseUrl}api/values/5"); });
}
Startup.cs (other items removed for brevity):
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}


