我正在Blazor中构建一个基本的CRUD应用程序,并在添加事件记录(在AddEvent.razor中)后将用户重定向到事件列表。发布请求成功通过,但执行没有返回,也没有点击我的array_columns
来重定向到“事件列表”页面。
我的Blazor解决方案在以下位置使用WebAssembly托管:
这是我在Program.cs中配置HttpClient的方式:
array_multisort
我有一个AddEvent.razor页面,其中通过NavigateTo
方法添加了事件实体:
builder.Services.AddSingleton( new HttpClient
{
BaseAddress = new Uri( builder.HostEnvironment.BaseAddress )
});
这是CreateEvent定义P
CreateEvent
我的 <div class="form-group">
<button class="btn btn-primary" @onclick="@CreateEvent">Save</button>
<button class="btn btn-warning" @onclick="@cancel">Cancel</button>
</div>
类具有以下Post方法:
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");
}
我的主要问题是在AddEvent.razor CreateEvent方法中,似乎在此行之后失去执行:
EventController
它从未命中以下导航线:
// POST api/<controller>
[HttpPost("create")]
public ActionResult Post([FromBody]Event evt)
{
if (ModelState.IsValid){
_eventRepository.AddEvent(evt);
return Ok();
}
return BadRequest();
}
我尝试更改EventController Post的签名,并将var response = await Http.PostAsJsonAsync("/api/Event/create", evt);
调用包装在try / catch中,但它从未命中异常块。我还尝试在发布后添加延迟,但没有运气...
这是我单击AddEvent按钮时在日志中看到的内容:
Navigation.NavigateTo("/fetchevent");
关于Http.PostAsJsonAsync
调用为何不返回并继续执行我的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
方法中其余语句的任何想法?