2

我需要在 blazor 托管应用程序中将数据从客户端传递到服务器端,但我无法实现这一点。

<button @onclick="PassData">Click me to send the data</button>

private async void PassData()
    {

        await Http.SendJsonAsync(HttpMethod.Post, "/api/Controller/Method", JsonObject);
    }

此方法不会在我的服务器控制器中命中。你能帮我做错什么吗?

4

1 回答 1

0

首先,确保将路由映射到控制器

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDefaultControllerRoute(); // this line

                endpoints.MapFallbackToClientSideBlazor<Client.Startup>("index.html");
            });

在控制器文件夹中创建一个新控制器

    [ApiController]
    [Route("api/[controller]")]
    public class TestController : ControllerBase
    { 
         [HttpPost]
         public string Post([FromBody]Data data)
         {
            // Handle here
         }
    }

叫它

Http.PostJsonAsync<Data>("api/Test", data);

希望能帮助到你

于 2019-09-25T09:14:16.750 回答