最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何在?ASP.NET?Core?Web?API?中處理?Patch?請求

 更新時間:2023年05月14日 15:47:11   作者:alby  
這篇文章主要介紹了在?ASP.NET?Core?Web?API中處理Patch請求,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、概述

PUT 和 PATCH 方法用于更新現(xiàn)有資源。 它們之間的區(qū)別是,PUT 會替換整個資源,而 PATCH 僅指定更改。

在 ASP.NET Core Web API 中,由于 C# 是一種靜態(tài)語言(dynamic 在此不表),當(dāng)我們定義了一個類型用于接收 HTTP Patch 請求參數(shù)的時候,在 Action 中無法直接從實例中得知客戶端提供了哪些參數(shù)。

比如定義一個輸入模型和數(shù)據(jù)庫實體:

public class PersonInput
{
    public string? Name { get; set; }
    public int? Age { get; set; }
    public string? Gender { get; set; }
}
public class PersonEntity
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Gender { get; set; }
}

再定義一個以 FromForm 形式接收參數(shù)的 Action:

[HttpPatch]
[Route("patch")]
public ActionResult Patch([FromForm] PersonInput input)
{
    // 測試代碼暫時將 AutoMapper 配置放在方法內(nèi)。
    var config = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap<PersonInput, PersonEntity>());
    });
    var mapper = config.CreateMapper();
    // entity 從數(shù)據(jù)庫讀取,這里僅演示。
    var entity = new PersonEntity
    {
        Name = "姓名", // 可能會被改變
        Age = 18, // 可能會被改變
        Gender = "我可能會被改變",
    };
    // 如果客戶端只輸入 Name 字段,entity 的 Age 和 Gender 將不能被正確映射或被置為 null。
    mapper.Map(input, entity);
    return Ok();
}
curl --location --request PATCH 'http://localhost:5094/test/patch' \
--form 'Name="foo"'

如果客戶端只提供了 Name 而沒有其他參數(shù),從 HttpContext.Request.Form.Keys 可以得知這一點。如果不使用 AutoMapper,那么接下來是丑陋的判斷:

var keys = _httpContextAccessor.HttpContext.Request.Form.Keys;
if(keys.Contains("Name"))
{
    // 更新 Name(這里忽略合法性判斷)
    entity.Name = input.Name!;
}
if (keys.Contains("Age"))
{
    // 更新 Age(這里忽略合法性判斷)
    entity.Age = input.Age!;
}
// ...

本文提供一種方式來簡化這個步驟。

二、將 Keys 保存在 Input Model 中

定義一個名為 PatchInput 的類:

public abstract class PatchInput
{
    [BindNever]
    public ICollection<string>? PatchKeys { get; set; }
}

PatchKeys 屬性不由客戶端提供,不參與默認(rèn)綁定。

PersonInput 繼承自 PatchInput:

public class PersonInput : PatchInput
{
    public string? Name { get; set; }
    public int? Age { get; set; }
    public string? Gender { get; set; }
}

三、定義 ModelBinderFactory 和 ModelBinder

public class PatchModelBinder : IModelBinder
{
    private readonly IModelBinder _internalModelBinder;
    public PatchModelBinder(IModelBinder internalModelBinder)
    {
        _internalModelBinder = internalModelBinder;
    }
    public async Task BindModelAsync(ModelBindingContext bindingContext)
    {
        await _internalModelBinder.BindModelAsync(bindingContext);
        if (bindingContext.Model is PatchInput model)
        {
            // 將 Form 中的 Keys 保存在 PatchKeys 中
            model.PatchKeys = bindingContext.HttpContext.Request.Form.Keys;
        }
    }
}
public class PatchModelBinderFactory : IModelBinderFactory
{
    private ModelBinderFactory _modelBinderFactory;
    public PatchModelBinderFactory(
        IModelMetadataProvider metadataProvider,
        IOptions<MvcOptions> options,
        IServiceProvider serviceProvider)
    {
        _modelBinderFactory = new ModelBinderFactory(metadataProvider, options, serviceProvider);
    }
    public IModelBinder CreateBinder(ModelBinderFactoryContext context)
    {
        var modelBinder = _modelBinderFactory.CreateBinder(context);
        // ComplexObjectModelBinder 是 internal 類
        if (typeof(PatchInput).IsAssignableFrom(context.Metadata.ModelType)
            && modelBinder.GetType().ToString().EndsWith("ComplexObjectModelBinder"))
        {
            modelBinder = new PatchModelBinder(modelBinder);
        }
        return modelBinder;
    }
}

四、在 ASP.NET Core 項目中替換 ModelBinderFactory

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddPatchMapper();

AddPatchMapper 是一個簡單的擴(kuò)展方法:

public static class PatchMapperExtensions
{
    public static IServiceCollection AddPatchMapper(this IServiceCollection services)
    {
        services.Replace(ServiceDescriptor.Singleton<IModelBinderFactory, PatchModelBinderFactory>());
        return services;
    }
}

到目前為止,在 Action 中已經(jīng)能獲取到請求的 Key 了。

[HttpPatch]
[Route("patch")]
public ActionResult Patch([FromForm] PersonInput input)
{
    // 不需要手工給 input.PatchKeys 賦值。
    return Ok();
}

PatchKeys 的作用是利用 AutoMapper。

五、定義 AutoMapper 的 TypeConverter

public class PatchConverter<T> : ITypeConverter<PatchInput, T> where T : new()
{
    /// <inheritdoc />
    public T Convert(PatchInput source, T destination, ResolutionContext context)
    {
        destination ??= new T();
        var sourceType = source.GetType();
        var destinationType = typeof(T);
        foreach (var key in source.PatchKeys ?? Enumerable.Empty<string>())
        {
            var sourcePropertyInfo = sourceType.GetProperty(key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
            if (sourcePropertyInfo != null)
            {
                var destinationPropertyInfo = destinationType.GetProperty(key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                if (destinationPropertyInfo != null)
                {
                    var sourceValue = sourcePropertyInfo.GetValue(source);
                    destinationPropertyInfo.SetValue(destination, sourceValue);
                }
            }
        }
        return destination;
    }
}

上述代碼可用其他手段來代替反射。

六、模型映射

[HttpPatch]
[Route("patch")]
public ActionResult Patch([FromForm] PersonInput input)
{
    // 1. 目前僅支持 `FromForm`,即 `x-www-form_urlencoded` 和 `form-data`;暫不支持 `FromBody` 如 `raw` 等。
    // 2. 使用 ModelBinderFractory 創(chuàng)建 ModelBinder 而不是 ModelBinderProvider 以便于未來支持更多的輸入格式。
    // 3. 目前還沒有支持多級結(jié)構(gòu)。
    // 4. 測試代碼暫時將 AutoMapper 配置放在方法內(nèi)。
    var config = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap<PersonInput, PersonEntity>().ConvertUsing(new PatchConverter<PersonEntity>());
    });
    var mapper = config.CreateMapper();
    // PersonEntity 有 3 個屬性,客戶端如果提供的參數(shù)參數(shù)不足 3 個,在 Map 時未提供參數(shù)的屬性值不會被改變。
    var entity = new PersonEntity
    {
        Name = "姓名",
        Age = 18,
        Gender = "如果客戶端沒有提供本參數(shù),那我的值不會被改變"
    };
    mapper.Map(input, entity);
    return Ok();
}

七、測試

curl --location --request PATCH 'http://localhost:5094/test/patch' \
--form 'Name="foo"'

curl --location --request PATCH 'http://localhost:5094/test/patch' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'Name=foo'

源碼

Tubumu.PatchMapper

  • 支持 FromForm,即 x-www-form_urlencoded 和 form-data
  • 支持 FromBody 如 raw 等。
  • 支持多級結(jié)構(gòu)。

參考資料

GraphQL.NET

如何在 ASP.NET Core Web API 中處理 JSON Patch 請求

到此這篇關(guān)于在 ASP.NET Core Web API 中處理 Patch 請求的文章就介紹到這了,更多相關(guān)ASP.NET Core Web API 處理 Patch 請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

扶余县| 阜康市| 宝鸡市| 赤水市| 峨眉山市| 满城县| 依安县| 页游| 三都| 聂荣县| 花莲县| 乌拉特前旗| 盖州市| 金溪县| 鄂尔多斯市| 桐乡市| 屯门区| 从化市| 化州市| 深圳市| 保定市| 正阳县| 林州市| 宽甸| 平乐县| 溧阳市| 齐齐哈尔市| 南投县| 波密县| 广元市| 沈阳市| 周口市| 海宁市| 淮北市| 绥宁县| 乃东县| 泾川县| 香港| 贵阳市| 石景山区| 上犹县|