I try to implement a simple onclick event handler like this sample, but it is not working in my solution. The event is only triggered at the run of the web page for unknown reasons.
The HTML page with Blazor component is well shown, but when I click on the button, nothing is happening.
I'm on Visual Studio 2019 and .NET Core 3.0, with an ASP.NET MVC project.
Counter.razor file
@using Microsoft.AspNetCore.Components<p>Current count: @currentCount</p><button class="btn btn-primary" onclick="@IncrementCount();">Click me</button>@code { int currentCount = 0; private async Task IncrementCount() { await Task.Run(() => currentCount++); }}File Index.cshtml
@using WebApplication2.Views.Components@{ ViewData["Title"] = "Home Page";}<div class="text-center"><h1 class="display-4">Welcome</h1><p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p></div>@(await Html.RenderComponentAsync<Counter>(RenderMode.Server, new { }))File startup.cs
public void ConfigureServices(IServiceCollection services){ services.AddControllersWithViews(); services.AddHttpClient(); services.AddRazorPages(); services.AddServerSideBlazor(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);}public void Configure(IApplicationBuilder app, IWebHostEnvironment env){ if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change // this for production scenarios; see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); endpoints.MapBlazorHub(); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });}The button in the browser:
<button class="btn btn-primary" onclick="System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[System.Threading.Tasks.VoidTaskResult,WebApplication2.Views.Components.Counter+<IncrementCount>d__2];">Click me</button>The error in the browser: