我试图将一个模型发布到我的MongoDB,但Blazor应用程序的Post命令甚至没有到达控制器中API的Post方法.我已经看到了一些关于这个问题的话题,也try 了几个小时来调试这个问题.
我想发布一个非常简单的模型.
public string MomentCategoryName { get; set; }
单击按钮可激活此方法.(我为这一个硬编码了一个CategoryName.)
public async Task PostAsync(MomentCategoryModel momentCategory)
{
using (HttpResponseMessage response = await _api.ApiClient.PostAsJsonAsync("api/MomentCategory/Create", momentCategory))
{
if (!response.IsSuccessStatusCode)
{
throw new Exception();
}
}
}
baseuri和客户机在另一个类中初始化,但整个uri如下所示:https://localhost:7141/api/MomentCategory/Create
MomentCategoryController
中的PostMomentCategory
从另一个库中获得MomentCategoryModel
:
[BsonId]
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
public string Id { get; set; }
public string MomentCategoryName { get; set; }
然后在这个过程中,这个代码应该运行,但编译器永远不会得到这个结果,它永远不会运行:
[HttpPost]
[Route("Create")]
public void PostMomentCategory(MomentCategoryModel model)
{
_momentCategoryData.CreateMomentCategory(model);
}
正因为如此,我收到了一个不好的请求.但当我执行一个几乎相同的GET请求时,它会正确运行.例子:
public async Task<List<MomentCategoryModel>> GetAllAsync()
{
using (HttpResponseMessage response = await _api.ApiClient.GetAsync("api/MomentCategory/GetAll"))
{
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadFromJsonAsync<List<MomentCategoryModel>>();
return result;
}
else
{
throw new Exception();
}
}
}
感谢您的帮助和提前抽出的时间!