用于更新操作的类似CreatedAtAction

时间:2018-12-24 09:38:32

标签: c# .net rest .net-core asp.net-core-webapi

有时候,我允许使用原始数据创建/更新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方法获取状态

1 个答案:

答案 0 :(得分:1)

您可以像CreatedAtAction一样实现自己的UpdatedAtAction

  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 });
        }
    }