2015-06-22 71 views
0

這是我在stackoverflow上的第一個問題,所以如果標題或問題不好,請原諒。Kentico圖像API - 需要在ASP.NET Web API中返回圖像URL而不是Guid

在我VoucherResponse類

public class VoucherResponse 
{ 
    public int VoucherID { get; set; } 
    public string VoucherTitle { get; set; } 
    public IEnumerable<VoucherVariant> VoucherVariants { get; set; } 
    public string[] VoucherImages { get; set; } 
} 

和Web API方法

return Ok(from v in distinctby 
        select new VoucherResponse 
        { 
         VoucherID = v.VoucherID, 
         VoucherTitle = v.VoucherTitle, 
         VoucherVariants = (from voucher in voucherToLookup[v.VoucherID] 
              select new VoucherVariant 
              { 
               VoucherDiscountedPrice = voucher.VoucherPrice - (voucher.VoucherPrice * voucher.VoucherDiscount/ 100), 
               VoucherVariantId = voucher.VoucherVariantId, 
               VoucherPrice = voucher.VoucherPrice, 
               VoucherStock = 1 

              }), 
         VoucherImages = (from image in distinctby 
              where v.VoucherID == image.VoucherID 
              select image.VoucherImages 

              // URLHelper.GetAbsoluteUrl(ModuleCommands.MediaLibraryGetMediaFileUrl(image.FileGUID, voucher.NodeAlias + "_" + image.ImageOrder))).ToArray(), 
             ).ToArray() 
        }); 

目前我得到的結果的那一刻

{ 
"voucherID": 380, 
"voucherTitle": "Woolworths e-Gift Card", 
"voucherVariants": [ 
    { 
    "voucherVariantId": 397, 
    "voucherPrice": 100, 
    "voucherDiscountedPrice": 95, 
    "voucherStock": 1 
    }, 
    { 
    "voucherVariantId": 389, 
    "voucherPrice": 200, 
    "voucherDiscountedPrice": 190, 
    "voucherStock": 1 
    }, 
    { 
    "voucherVariantId": 393, 
    "voucherPrice": 300, 
    "voucherDiscountedPrice": 285, 
    "voucherStock": 1 
    }, 
    { 
    "voucherVariantId": 394, 
    "voucherPrice": 400, 
    "voucherDiscountedPrice": 380, 
    "voucherStock": 1 
    }, 
    { 
    "voucherVariantId": 395, 
    "voucherPrice": 500, 
    "voucherDiscountedPrice": 475, 
    "voucherStock": 1 
    } 
], 
"voucherImages": [ 
    " B4A3A070-3ED1-4086-8B33-21465AA58C7A, 2FB01963-2D71-4C79-9EE8-38A114CAF1A6" 
] 

我需要回到這樣voucherImages

"VoucherImages" : [ 
{"www.kentico.info/imageOne.jpg"} 
{"www.kentico.info/imageTwo.jpg"} 
] 
+0

問題是什麼?你爲什麼不這樣格式化數據並返回? – JotaBe

+0

問題出在標題和最後一段。不知道如何使它工作 –

+0

你在哪裏存儲圖像? (媒體庫/附件/文件頁面類型...?)或者您使用哪種表單控件來存儲圖像? – rocky

回答

0

首先謝謝你試圖幫助我。 我自己解決了這個問題。 @rocky圖像存儲在媒體庫中,我創建了接縫要工作,但我仍然得到「醜陋網址」像 法「http://localhost:50848/getmedia/8f5f2e87-782c-4ee2-b6c0-dba614588fe4/ 我想獲得類似」 http://localhost:50848/getmedia/image1.jpg 但會做這項研究,如果有人需要代碼中的功能,我將粘貼方法如下

private string[] GetImageURLs(string guids, string voucherTitle) 
    { 
     List<string> result = new List<string>(); 
     string[] guid = guids.Split(','); 
     string alias, url,image; 

     foreach (var item in guid) 
     { 
      url = ModuleCommands.MediaLibraryGetMediaFileUrl(ValidationHelper.GetGuid(item, Guid.Empty), SiteContext.CurrentSiteName); 
      image = URLHelper.GetAbsoluteUrl(url); 
      result.Add(image); 
     } 

     return result.ToArray(); 
    }