簡體   English   中英

用於更新操作的類似CreatedAtAction

[英]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方法的實現看起來如何? 所以我實際上想要三件事:

  1. 退貨狀態200
  2. 取得更新狀態DTO
  3. 提供正確的位置標頭,以便以后通過GET方法獲取狀態

您可以實現自己的UpdatedAtAction例如CreatedAtAction

  1. 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; } } } 
  2. 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); } 
  3. 使用率

     [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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM