asp.net-core - 在 Blazor 中发送 POST 请求后丢失 UI 线程
问题描述
我正在 Blazor 中构建一个基本的 CRUD 应用程序,并在添加事件记录(在 AddEvent.razor 中)后,我想将用户重定向到事件列表。Post 请求通过,但执行没有回来,也没有点击我NavigateTo
重定向到事件列表页面。
我的 Blazor 解决方案是在我拥有的地方使用 WebAssembly 托管:
- 客户端项目(.NET Standard 2.1)
- 使用 EntityFrameworkCore 3.1.8 和 Microsoft.AspNetCore 3.2.1 的服务器项目 REST API (dotnet core 3.1)
- 共享项目(.NET Standard 2.1)
这就是我在 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
关于为什么PostAsJsonAsync
call 没有返回并继续我的方法中的其余语句的任何想法CreateEvent
?
解决方案
推荐阅读
- java - 创建 com.linecorp.armeria.spring.ArmeriaAutoConfiguration 中定义的名称为“armeriaServer”的 bean 时出错
- python - 在python中计算抵押贷款的利率
- timer - 通过 STM32F446RE 定时器在 Arduino IDE 中的备用引脚上进行 PWM
- c - 使用 ATmega88 从编码器脉冲信号 C 控制直流电机低速
- javascript - 防止在小滑块中的两个轴上滚动
- python - “更改密码系统”在用户身份验证中不起作用。(Django)
- android - 如何将图像的下载 URL 设置为 Firestore 数据库和实时数据库
- postgresql - 在 postgresql 中的存储过程上调用 PERFORM 时出现语法错误
- xml - WSProxy:尝试调用 SOAP 服务的嵌套错误错误
- yocto - yocto:将标准包移到另一个地方