簡體   English   中英

ASP.NET中的Web服務

[英]Web Services in ASP.NET

任何人都可以向我解釋如何將這種情況作為Web服務方法使用(我將本練習用於學習目的)。

物品清單及其價格; book 0.50notepad 1.20 然后是用戶鍵入金額的一種方式,因此,如果他們在文本框中鍵入1,如果他們鍵入2並選擇記事本,則該金額為2.40。

為此,我需要兩個Web方法嗎? 存放東西的最佳方法是什么? 我已經嘗試過了,但是出現了我沒有重載方法的錯誤:

[WebMethod]
public ThingsPrices[] GetThings(string thing, decimal price)
{
    List<ThingsPrices> things = new List<ThingsPrices>();
    things.Add(new  ("book", 0.50);
    things.Add("notepad", 1.20); //No overload for method Add??
    return things.ToArray(); 
} 

您的代碼中存在幾個不同的問題,因此我不太清楚您要實現的目標。 首先讓我們修復代碼,然后解決其他問題。 您的代碼有一些錯誤。 下面應該可以正常運行:

[WebMethod]
public IEnumerable<ThingsPrice> GetThings(string thing, decimal price)
{
    List<ThingsPrices> things = new List<ThingsPrices>();
    things.Add(new ThingsPrice {"book", 0.50});
    things.Add(new ThingsPrice {"notepad", 1.20}); 
    return things; 
} 

您可以針對您的方案使用集合初始化程序:

public class ThingsPrices
    {
        public string ItemName { get; set; }
        public double Price { get; set; }
    }

public ThingsPrices[] GetThings()
        {

            var things = new List<ThingsPrices>()
            {
                new ThingsPrices
                {
                    ItemName = "book",
                    Price = 0.5
                },
                new ThingsPrices
                {
                    ItemName = "notepad",
                    Price = 1.2
                }
            };

            return things.ToArray();
        }

在此處閱讀有關此內容的更多信息: https : //msdn.microsoft.com/en-us/library/bb397680.aspx

這是總價格計算:

public class Amount
    {
        public int Quantity { get; set; }
        public ThingsPrices ItemType { get; set; }        
    }    

    public static List<Amount> InputItems = new List<Amount>();

    public class AmmountCalculator
    {       
        public double GetTotalAmmount()
        {
            return InputItems.Sum(i => i.ItemType.Price*i.Quantity);
        }
    }

暫無
暫無

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

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