簡體   English   中英

如何在 POST 請求的響應中顯示 null 值?

[英]How I can show null value in the response from a POST request?

我有一個 POST: localhost:10411/api/channel -> 使用請求正文創建通道:

"cut": {
{
"title": "track 200-1",
"duration": "00:00:30",
"album": "album 200-1",
"logoUrl":null
},
{
"title": "track 200-2",
"duration": "00:00:30",
"album": "album 200-2",
"logoUrl": "this is a text"
},
{
"title": "track 200-3",
"duration": "00:00:30",
"album": "album 200-3",
}
}

我想要什么時候:

  1. 沒有提供 logoUrl -> 它將生成圖像;

  2. 提供的 logoUrl 字符串 -> 在響應中使用文本;

  3. logoUrl null 提供 -> 在響應中使用 null;

但是,我收到的響應僅滿足 1) 和 2) 條件。 我無法讓 3) 工作。 這是回應:

{
"title": "track 200-1",
"album": "album 200-1",
"url": "localhost:10411/api/logos/album%200-1" (I want here show url = null instead of showing auto generate logo)
},
{
"title": "track 200-2",
"album": "album 200-2",
"url": "this is a text" (showing string, worked as expected)
},
{
"title": "track 200-3",
"duration": "00:00:30",
"album": "album 200-3",
"url": "localhost:10411/api/logos/album%200-3" (showed auto generate logo when not specify logoUrl in the request body, worked as expected)
}

這是我的映射和 function:

CreateMap<Cut, CutData>()
   .ForMember(
       dest => dest.Album,
       opt => opt.MapFrom(scr => GetAlbum(scr.Channel.BaseUrl, src.LogoUrl, src.Album)));

private Album GetAlbum(string baseUrl, string customUrl, string albumName)
{
  Album album = new Album(baseUrl, albumName);
  if(customUrl != null)
  {
    image.Url = customUrl;
  }

  return album;
}

當我沒有在請求正文中指定 logoUrl(logoUrl 完全不存在)時,請大家幫助我如何區分這種情況。->它將生成現在運行良好的自動徽標。 當我指定 logoUrl = null -> 我希望它顯示在響應 null 而不是顯示自動徽標時的情況。 非常感謝您的幫助。 由衷的贊賞!

不幸的是你不能。 當您將 object 發送到 API 時。 任何未提供的值都將自動成為default值 - 對於null的字符串。 意義:

{
   "title": "track 200-1",
   "duration": "00:00:30",
   "album": "album 200-1",
   "logoUrl":null
}

{
   "title": "track 200-2",
   "album": "album 200-2",
   "url": "this is a text" 
}

是相同的。

如果你真的想要第三個值,你可以logoUrl: "" 這樣你就可以這樣說:

if (logoUrl == null) {/* Do Work */}
if (logoUrl.Length == 0) {/* Do Work */}
if (logoUrl.Length > 0) {/* Do Work */}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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