-
HttpHead:1、与GET类似,但没要响应主体;2、检测缓存;3、探测资源是否存在。
用法:在[HttpGet]方法的attribute下加一个[HttpHead]即可,visual studio会自动识别并把httpGet请求转化为HttpHead请求,即没有响应主体的Get。 -
[HttpPut]更新对象。
参考AutoMapper
[HttpPut("{touristRouteId}", Name = "UpdateTouristRouteAsync")]
public async Task<IActionResult> UpdateTouristRoute([FromRoute] Guid touristRouteId, [FromBody] TouristRouteForUpdateDto touristRouteForUpdateDto)
{
if (!await _touristRouteRepository.TouristRouteExistAsync(touristRouteId))
{
return NotFound("旅游路线找不到");
}
var touristRouteFromRepo = await _touristRouteRepository.TouristRouteGetRouteAsync(touristRouteId);
//1、映射Dto;2、更新Dto;3、映射model
_mapper.Map(touristRouteForUpdateDto, touristRouteFromRepo);//这一句完成了三步,不是很懂,待补充https://github.com/AutoMapper/AutoMapper/blob/bdc0120497d192a2741183415543f6119f50a982/src/AutoMapper/Mapper.cs
await _touristRouteRepository.SaveAsync();
return NoContent();
}
- [HttpPatch]
依赖库:Microsoft.AspNetCore.JsonPatch、Microsoft.AspNetCore.Mvc.NewtonsoftJson
Patch发送的body数据格式:
{
{
"op":"replace",//op操作只允许:add——添加字段,remove——删除字段,replace——替换字段数据,move——转移字段,copy——复制字段,test——测试
"path":"/titel",//必须有path
"value":"ABCD"
},
{
"op":"replace",
"path":"/desciption",
"value":"ABCD"
}
}
在使用Json前需要先在startup里注入Json服务
services.AddControllers().AddNewtonsoftJson(setupAction => {setupAction.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();}).AddXmlDataContractSerializerFormatters()
然后再调用
[HttpPatch("{touristRouteId}", Name = "PartiallyUpdateTouristRouteAsync")]
public async Task<IActionResult> PartiallyUpdateTouristRoute([FromRoute] Guid touristRouteId, [FromBody] JsonPatchDocument<TouristRouteForUpdateDto> patchDocument)
{
if (!await _touristRouteRepository.TouristRouteExistAsync(touristRouteId))
{
return NotFound("旅游路线找不到");
}
var touristRouteFromRepo = await _touristRouteRepository.TouristRouteGetRouteAsync(touristRouteId);
var touristRouteToPatch = _mapper.Map<TouristRouteForUpdateDto>(touristRouteFromRepo);
patchDocument.ApplyTo(touristRouteToPatch, ModelState);
if (!TryValidateModel(touristRouteToPatch))//此处对touristRouteToPatch进行数据校验,不懂。
{
return ValidationProblem(ModelState);
}
_mapper.Map(touristRouteToPatch, touristRouteFromRepo);//Map的操作依旧不懂,与put一样
await _touristRouteRepository.SaveAsync();
return NoContent();
}
- [HttpDelete]