首页 > 解决方案 > 在 Blazor 中发送 POST 请求后丢失 UI 线程

问题描述

我正在 Blazor 中构建一个基本的 CRUD 应用程序,并在添加事件记录(在 AddEvent.razor 中)后,我想将用户重定向到事件列表。Post 请求通过,但执行没有回来,也没有点击我NavigateTo重定向到事件列表页面。

我的 Blazor 解决方案是在我拥有的地方使用 WebAssembly 托管:

这就是我在 Program.cs 中配置 HttpClient 的方式:

builder.Services.AddSingleton( new HttpClient
{
   BaseAddress = new Uri( builder.HostEnvironment.BaseAddress )
});

我有一个 AddEvent.razor 页面,我通过以下CreateEvent方法添加了一个 Event 实体:

    <div class="form-group">
        <button class="btn btn-primary" @onclick="@CreateEvent">Save</button>
        <button class="btn btn-warning" @onclick="@cancel">Cancel</button>
    </div>

这是 CreateEvent 定义P

private async Task CreateEvent()
{
    try
    {
        //While debugging seems like UI thread is lost after this line, it doesn't reach the Navigation statement below
        var response = await Http.PostAsJsonAsync("/api/Event/create", evt);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

    await Task.Delay(500);
    Navigation.NavigateTo("/fetchevent");
}

我的EventController班级有这个 Post 方法:

// POST api/<controller>
[HttpPost("create")]
public ActionResult Post([FromBody]Event evt)
{
    if (ModelState.IsValid){
        _eventRepository.AddEvent(evt);
        return Ok();
    }
    return BadRequest();
}

我的主要问题是在 AddEvent.razor CreateEvent 方法中,在此行之后执行似乎丢失了:

var response = await Http.PostAsJsonAsync("/api/Event/create", evt);

它永远不会碰到下面的导航线:

Navigation.NavigateTo("/fetchevent");

我尝试更改 EventController Post 的签名并将Http.PostAsJsonAsync调用包装在 try/catch 中,但它从未遇到异常块。我还尝试在发布后添加延迟,但没有运气......

这是我单击 AddEvent 按钮时在日志中看到的内容:

Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/1.1 POST http://localhost:54114/api/Event/create application/json; charset=utf-8 105
Microsoft.AspNetCore.Routing.EndpointMiddleware: Information: Executing endpoint 'Paycor.PartnerManager.Server.Controllers.EventController.Post (Paycor.PartnerManager.Server)'
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Information: Route matched with {action = "Post", controller = "Event"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.ActionResult Post(Paycor.PartnerManager.Shared.Models.Event) on controller Paycor.PartnerManager.Server.Controllers.EventController (Paycor.PartnerManager.Server).
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/1.1 GET http://localhost:54114/addevent?  
Microsoft.AspNetCore.Routing.EndpointMiddleware: Information: Executing endpoint 'Fallback {*path:nonfile}'
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Information: Sending file. Request path: '/index.html'. Physical path: 'C:\Development\GitRepos\Prototypes\Paycor.PartnerManager\PartnerManager\Client\wwwroot\index.html'
Microsoft.AspNetCore.Routing.EndpointMiddleware: Information: Executed endpoint 'Fallback {*path:nonfile}'
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished in 9.8383ms 200 text/html
Debugging hotkey: Shift+Alt+D (when application has focus)
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/1.1 GET http://localhost:54114/_framework/blazor.boot.json  
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Information: The file /_framework/blazor.boot.json was not modified
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished in 27.6226ms 304 application/json
Microsoft.AspNetCore.Mvc.StatusCodeResult: Information: Executing HttpStatusCodeResult, setting HTTP status code 200
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Information: Executed action Paycor.PartnerManager.Server.Controllers.EventController.Post (Paycor.PartnerManager.Server) in 556.5854ms
Microsoft.AspNetCore.Routing.EndpointMiddleware: Information: Executed endpoint 'Paycor.PartnerManager.Server.Controllers.EventController.Post (Paycor.PartnerManager.Server)'
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished in 592.7471ms 0 

关于为什么PostAsJsonAsynccall 没有返回并继续我的方法中的其余语句的任何想法CreateEvent

标签: asp.net-core.net-coreblazor

解决方案


推荐阅读