gpt4 book ai didi

c# - 用于更新操作的 CreatedAtAction 模拟

转载 作者:行者123 更新时间:2023-12-03 16:24:12 24 4
gpt4 key购买 nike

有时我允许使用原始数据创建/更新物联网设备的状态。这意味着客户端可以将设备设备状态作为字节数组读取并通过 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 方法的实现会是什么样子?所以我实际上想要三件事:
  • 返回状态 200
  • 取回更新状态 DTO
  • 提供正确的位置 header ,以便稍后通过 GET 方法获取状态
  • 最佳答案

    您可以实现自己的 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;
    }

    }
    }
  • 我的 Controller 库
    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 });
    }
    }
  • 关于c# - 用于更新操作的 CreatedAtAction 模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53911629/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com