[英]Analog of CreatedAtAction for update operation
有時我允許使用原始數據創建/更新IoT設備的狀態。 這意味着客戶端可以讀取設備設備狀態(字節數組)並通過API發送該數據。 服務器解析的數據並作為常規DTO發送回去。
對於創建,我可能會引入以下CreateStatusFromRawData
方法:
[HttpGet("{id}/status")]
[ProducesResponseType(200, Type = typeof(DeviceStatus))]
[ProducesResponseType(404)]
public async Task<IActionResult> GetStatus(Guid id)
{
// gets the device status
}
[HttpPost("{id}/status/rawdata")]
[ProducesResponseType(201, Type = typeof(DeviceStatus))]
[ProducesResponseType(404)]
public async Task<IActionResult> CreateStatusFromRawData(Guid id, [FromBody]byte[] rawdata)
{
// some parsing logic
return CreatedAtAction(nameof(GetStatus), new {id})
}
我想對更新操作進行相同操作:
[HttpPut("{id}/status/rawdata")]
[ProducesResponseType(200, Type = typeof(DeviceStatus))]
[ProducesResponseType(404)]
public async Task<IActionResult> UpdateStatusFromRawData(Guid id, [FromBody]byte[] rawdata)
{
// some parsing logic
return **UpdatedAtAction**(nameof(GetStatus), new {id})
}
UpdatedAtAction方法的實現看起來如何? 所以我實際上想要三件事:
您可以實現自己的UpdatedAtAction
例如CreatedAtAction 。
UpdatedAtActionResult
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Net.Http.Headers; using System; namespace MVCPro.CustomResult { public class UpdatedAtActionResult : ObjectResult { private const int DefaultStatusCode = StatusCodes.Status200OK; public UpdatedAtActionResult( string actionName, string controllerName, object routeValues, object value) : base(value) { ActionName = actionName; ControllerName = controllerName; RouteValues = routeValues == null ? null : new RouteValueDictionary(routeValues); StatusCode = DefaultStatusCode; } /// <summary> /// Gets or sets the <see cref="IUrlHelper" /> used to generate URLs. /// </summary> public IUrlHelper UrlHelper { get; set; } /// <summary> /// Gets or sets the name of the action to use for generating the URL. /// </summary> public string ActionName { get; set; } /// <summary> /// Gets or sets the name of the controller to use for generating the URL. /// </summary> public string ControllerName { get; set; } /// <summary> /// Gets or sets the route data to use for generating the URL. /// </summary> public RouteValueDictionary RouteValues { get; set; } /// <inheritdoc /> public override void OnFormatting(ActionContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } base.OnFormatting(context); var request = context.HttpContext.Request; var urlHelper = UrlHelper; if (urlHelper == null) { var services = context.HttpContext.RequestServices; urlHelper = services.GetRequiredService<IUrlHelperFactory>().GetUrlHelper(context); } var url = urlHelper.Action( ActionName, ControllerName, RouteValues, request.Scheme, request.Host.ToUriComponent()); if (string.IsNullOrEmpty(url)) { throw new InvalidOperationException("NoRoutesMatched"); } context.HttpContext.Response.Headers[HeaderNames.Location] = url; } } }
MyControllerBase
public class MyControllerBase: Controller { [NonAction] public virtual UpdatedAtActionResult UpdatedAtAction(string actionName, object value) => UpdatedAtAction(actionName, routeValues: null, value: value); [NonAction] public virtual UpdatedAtActionResult UpdatedAtAction(string actionName, object routeValues, object value) => UpdatedAtAction(actionName, controllerName: null, routeValues: routeValues, value: value); [NonAction] public virtual UpdatedAtActionResult UpdatedAtAction( string actionName, string controllerName, object routeValues, object value) => new UpdatedAtActionResult(actionName, controllerName, routeValues, value); }
使用率
[Route("api/User")] public class UserApiController : MyControllerBase { [HttpGet("{id}/status")] [ProducesResponseType(200, Type = typeof(DeviceStatus))] [ProducesResponseType(404)] public async Task<IActionResult> GetStatus(Guid id) { // gets the device status return Ok(new DeviceStatus { DeviceId = id }); } [HttpPost("{id}/status/rawdata")] [ProducesResponseType(201, Type = typeof(DeviceStatus))] [ProducesResponseType(404)] public async Task<IActionResult> CreateStatusFromRawData(Guid id, [FromBody]byte[] rawdata) { // some parsing logic return CreatedAtAction(nameof(GetStatus), new { id }); } [HttpPut("{id}/status/rawdata")] [ProducesResponseType(200, Type = typeof(DeviceStatus))] [ProducesResponseType(404)] public async Task<IActionResult> UpdateStatusFromRawData(Guid id, [FromBody]byte[] rawdata) { // some parsing logic return UpdatedAtAction(nameof(GetStatus), new { id }); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.