第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Postman 表單數(shù)據(jù)到 ASP.NET Core 2.2 控制器未綁定到具有私有設(shè)置器的命令

Postman 表單數(shù)據(jù)到 ASP.NET Core 2.2 控制器未綁定到具有私有設(shè)置器的命令

C#
慕沐林林 2023-04-16 10:05:25
工具視覺(jué)工作室 2017ASP.NET 核心 2.2郵遞員 v7.2.0我想做什么將 FormData 從 Postman 發(fā)送到 ASP.NET Core 控制器,并將來(lái)自請(qǐng)求的數(shù)據(jù)綁定到具有私有設(shè)置器屬性的命令類。我已經(jīng)使用相同的設(shè)置(私有設(shè)置器)毫無(wú)問(wèn)題地發(fā)送了 JSON 數(shù)據(jù)。該FromBody屬性將 JSON 字符串反序列化為模型而不會(huì)出現(xiàn)錯(cuò)誤。問(wèn)題如果模型具有私有設(shè)置器,則原始類型的屬性不會(huì)綁定。但是,復(fù)雜類型與訪問(wèn)修飾符無(wú)關(guān)??刂破鱗HttpPost][ProducesResponseType((int)HttpStatusCode.OK)][ProducesResponseType((int)HttpStatusCode.BadRequest)]public async Task<IActionResult> CreateItemAsync([FromForm]CreateItemCommand command){    bool result = false;    commandResult = await _mediator.Send(command);    if (!commandResult)    {        return BadRequest();    }    return Ok();}命令注意:該Title屬性已被故意保留為 public setter 以說(shuō)明行為[DataContract]public class CreateItemCommand    :IRequest<bool>{    [DataMember]    public string Title { get; set; }    [DataMember]    public string Description { get; private set; }    [DataMember]    public int Count { get; private set; }    [DataMember]    public HashSet<string> Tags { get; private set; }    [DataMember]    public string ItemDate { get; private set; }    [DataMember]    public List<IFormFile> Documents { get; private set; }    public CreateItemCommand()    {        Skills = new HashSet<string>();        Systems = new HashSet<string>();    }    public CreateItemCommand(string title, string description,         int count, HashSet<string> tags, string itemDate,         List<IFormFile> documents)        : this()    {        Title = title;        Description = description;        Count = count        Tags = tags;        ItemDate = itemDate;        Documents = documents;    }}在 Postman 中,我現(xiàn)在將請(qǐng)求設(shè)置如下:我不得不混淆一些信息,但您可以看到?jīng)]有設(shè)置帶有私有 setter 的原始類型。問(wèn)題為什么屬性訪問(wèn)修飾符只影響原始類型的屬性?為什么參數(shù)屬性設(shè)置為時(shí)會(huì)出現(xiàn)這種情況,F(xiàn)romForm設(shè)置為時(shí)卻不會(huì)FromBody
查看完整描述

1 回答

?
繁星點(diǎn)點(diǎn)滴滴

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊

為什么屬性訪問(wèn)修飾符只影響原始類型的屬性?

對(duì)于 Asp.Net Core ModelBinder,它將通過(guò)下面的ComplexTypeModelBinder代碼檢查該屬性是否為私有訪問(wèn)設(shè)置器:

protected virtual object CreateModel(ModelBindingContext bindingContext)

{

? ? if (bindingContext == null)

? ? {

? ? ? ? throw new ArgumentNullException(nameof(bindingContext));

? ? }


? ? // If model creator throws an exception, we want to propagate it back up the call stack, since the

? ? // application developer should know that this was an invalid type to try to bind to.

? ? if (_modelCreator == null)

? ? {

? ? ? ? // The following check causes the ComplexTypeModelBinder to NOT participate in binding structs as

? ? ? ? // reflection does not provide information about the implicit parameterless constructor for a struct.

? ? ? ? // This binder would eventually fail to construct an instance of the struct as the Linq's NewExpression

? ? ? ? // compile fails to construct it.

? ? ? ? var modelTypeInfo = bindingContext.ModelType.GetTypeInfo();

? ? ? ? if (modelTypeInfo.IsAbstract || modelTypeInfo.GetConstructor(Type.EmptyTypes) == null)

? ? ? ? {

? ? ? ? ? ? var metadata = bindingContext.ModelMetadata;

? ? ? ? ? ? switch (metadata.MetadataKind)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? case ModelMetadataKind.Parameter:

? ? ? ? ? ? ? ? ? ? throw new InvalidOperationException(

? ? ? ? ? ? ? ? ? ? ? ? Resources.FormatComplexTypeModelBinder_NoParameterlessConstructor_ForParameter(

? ? ? ? ? ? ? ? ? ? ? ? ? ? modelTypeInfo.FullName,

? ? ? ? ? ? ? ? ? ? ? ? ? ? metadata.ParameterName));

? ? ? ? ? ? ? ? case ModelMetadataKind.Property:

? ? ? ? ? ? ? ? ? ? throw new InvalidOperationException(

? ? ? ? ? ? ? ? ? ? ? ? Resources.FormatComplexTypeModelBinder_NoParameterlessConstructor_ForProperty(

? ? ? ? ? ? ? ? ? ? ? ? ? ? modelTypeInfo.FullName,

? ? ? ? ? ? ? ? ? ? ? ? ? ? metadata.PropertyName,

? ? ? ? ? ? ? ? ? ? ? ? ? ? bindingContext.ModelMetadata.ContainerType.FullName));

? ? ? ? ? ? ? ? case ModelMetadataKind.Type:

? ? ? ? ? ? ? ? ? ? throw new InvalidOperationException(

? ? ? ? ? ? ? ? ? ? ? ? Resources.FormatComplexTypeModelBinder_NoParameterlessConstructor_ForType(

? ? ? ? ? ? ? ? ? ? ? ? ? ? modelTypeInfo.FullName));

? ? ? ? ? ? }

? ? ? ? }


? ? ? ? _modelCreator = Expression

? ? ? ? ? ? .Lambda<Func<object>>(Expression.New(bindingContext.ModelType))

? ? ? ? ? ? .Compile();

? ? }


? ? return _modelCreator();

}

為什么當(dāng)參數(shù)屬性設(shè)置為 FromForm 時(shí)會(huì)出現(xiàn)這種情況,而設(shè)置為 FromBody 時(shí)不會(huì)出現(xiàn)這種情況


對(duì)于FromBody,它用于JsonInputFormatter從正文請(qǐng)求中綁定模型,它用于JsonConvert.DeserializeObject反序列化對(duì)象并Newtonsoft.Json支持從 json 字符串中反序列化包含私有 setter 的對(duì)象。


查看完整回答
反對(duì) 回復(fù) 2023-04-16
  • 1 回答
  • 0 關(guān)注
  • 155 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)