[英]JSON Patch HTTP request from client to API (published on AWS) returns "StatusCode: 405, ReasonPhrase: 'Method Not Allowed'"
I have an ASP.NET Core Web API published to Amazon ECS using AWS Fargate with working PATCH request that I have successfully tested using POSTMAN. Now I am trying to make that request in the client side application by following this .我有一个 ASP.NET 核心 Web API 使用 AWS Fargate 发布到 Amazon ECS,我已经使用 POSTMAN 成功测试了工作 PATCH 请求。现在我试图通过遵循此在客户端应用程序中发出该请求。
What I have on client side is this:我在客户端的是这样的:
public async Task<ActionResult> Patch(int companyId, string description)
{
JsonPatchDocument<CompanyInfo> patchDoc = new JsonPatchDocument<CompanyInfo>();
patchDoc.Replace(e => e.Description, description);
var jsonSerializeObject = JsonConvert.SerializeObject(patchDoc);
Debug.WriteLine(jsonSerializeObject);
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, "api/CompanyInfo/" + companyId)
{
Content = new StringContent(jsonSerializeObject, Encoding.Unicode, "application/json")
};
response = await _httpClient.SendAsync(request);
Debug.WriteLine(response);
return RedirectToAction(nameof(Index));
}
This is what I get in my response:这是我在回复中得到的:
StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Date: Sat, 26 Nov 2022 06:06:08 GMT
Connection: keep-alive
Server: Kestrel
Content-Length: 0
Allow: GET, PUT
}
As previously mentioned I have already confirmed the following json patch document using POSTMAN:如前所述,我已经使用POSTMAN确认了以下json补丁文件:
[
{
"value":"some text value",
"path":"/Description",
"op":"replace"
}
]
The API: API:
[HttpPatch]
public async Task<ActionResult> PartiallyUpdateCompanyInfo(int companyId, JsonPatchDocument<CompanyInfoForPatchDto> patchDocument)
{
var companyEntity = await _moviePlanetRepository.GetCompanyById(companyId, false);
if (companyEntity == null)
{
return NotFound();
}
var companyToPatch = _mapper.Map<CompanyInfoForPatchDto>(companyEntity);
patchDocument.ApplyTo(companyToPatch, ModelState);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (!TryValidateModel(companyToPatch))
{
return BadRequest(ModelState);
}
_mapper.Map(companyToPatch, companyEntity);
await _moviePlanetRepository.Save();
return NoContent();
}
add [FromBody] attribute添加 [FromBody] 属性
[HttpPatch]
public async Task<ActionResult> PartiallyUpdateCompanyInfo(int companyId, [FromBody]JsonPatchDocument<CompanyInfoForPatchDto> patchDocument)
{
var companyEntity = await _moviePlanetRepository.GetCompanyById(companyId, false);
if (companyEntity == null)
{
return NotFound();
}
var companyToPatch = _mapper.Map<CompanyInfoForPatchDto>(companyEntity);
patchDocument.ApplyTo(companyToPatch, ModelState);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (!TryValidateModel(companyToPatch))
{
return BadRequest(ModelState);
}
_mapper.Map(companyToPatch, companyEntity);
await _moviePlanetRepository.Save();
return NoContent();
}
Had to specify that the companyId
was a required for the Route.必须指定
companyId
是路由所必需的。 From this从此
It wasn't sufficient to provide the above attributes to the methods.
向方法提供上述属性是不够的。 It was required to provide the methods' address (and query string, resp.) parameters for these attributes ([HttpPut("{id}")], [HttpDelete("{id}")]), too.
还需要为这些属性([HttpPut("{id}")]、[HttpDelete("{id}")])提供方法的地址(和查询字符串,resp.)参数。 (This is particular to ASP.NET Core.)
(这是 ASP.NET Core 特有的。)
[Route("{companyId:int}")]
[HttpPatch]
public async Task<ActionResult> PartiallyUpdateCompanyInfo(int companyId, [FromBody]JsonPatchDocument<CompanyInfoForPatchDto> patchDocument)
{
var companyEntity = await _moviePlanetRepository.GetCompanyById(companyId, false);
if (companyEntity == null)
{
return NotFound();
}
var companyToPatch = _mapper.Map<CompanyInfoForPatchDto>(companyEntity);
patchDocument.ApplyTo(companyToPatch, ModelState);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (!TryValidateModel(companyToPatch))
{
return BadRequest(ModelState);
}
_mapper.Map(companyToPatch, companyEntity);
await _moviePlanetRepository.Save();
return NoContent();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.