有没有更有效的方法来处理 C# ASP.NET(尤其是 MVC 5)上的亚马逊产品广告 API?

2023-12-10

我终于在我的 MVC 5 网站上使用了 Amazon 产品广告 API。我正在使用亚马逊网站的下载之一中提供的“SignedRequestHelper”类。我实际上已经获得了 Amazon API 的参考,但我目前似乎根本没有使用它。

到目前为止我使用的是(控制器):

    SignedRequestHelper helper = new SignedRequestHelper("myAWSaccessKeyID",
    "mysecretKey", "webservices.amazon.co.uk");


    Dictionary<String, String> items = new Dictionary<String, String>();

    items.Add("Service", "AWSECommerceService");
    items.Add("Operation", "ItemSearch");
    items.Add("AWSAccessKeyId", "myAWSaccessKeyID");
    items.Add("AssociateTag", "myTag");
    items.Add("SearchIndex", SearchIndex);//This is a string value (selectbox)
    items.Add("ResponseGroup", "Images,ItemAttributes,OfferFull,Offers,OfferSummary,Reviews");
    items.Add("Keywords", keyword);//This is a string value

    string requestUrl = helper.Sign(items);

    ViewBag.Stuff = requestUrl;//Just so I could see the whole URL!

    WebRequest request = HttpWebRequest.Create(requestUrl);
    WebResponse response = request.GetResponse();
    XmlDocument doc = new XmlDocument();
    doc.Load(response.GetResponseStream());

    XmlNodeList titleNodes = doc.GetElementsByTagName("Item");

    ViewBag.Titles = titleNodes;

你可能会注意到我部分地从草稿本复制了JAVA代码的风格。

从那时起,我就只处理每个部分。处理这样的开关有点混乱和可怕:

foreach (System.Xml.XmlNode item in ViewBag.Titles)
{
    <h3>Item: @count</h3>
    foreach (System.Xml.XmlNode child in item.ChildNodes)
    {
        switch (child.Name)
        {
            case "ASIN":
                <p>ASIN: @child.InnerText</p>
                break;
            case "MediumImage":
                <img src="@child.ChildNodes[0].InnerText" />
                break;
            case "ItemAttributes":
                foreach (System.Xml.XmlNode child1 in child.ChildNodes)
                {
                    if(child1.Name == "Title")
                    {
                        <p>@child1.InnerText</p>
                    }
                }
                break;
        }

    }
    count++;
}

它可以工作,我可以使用 XML 文档等。我只需要知道是否有办法更改它,以便它实际上使用作为参考给出的 API 部分。我宁愿使用适当的工具,也不愿使用像这样的原始 XML。我在连接 Amazon 文档时遇到了很大的困难,以至于我基本上只是尝试在 Amazon 暂存器上的 JAVA 样式代码中进行连接。


您可以使用以下 nugetNager.Amazon产品广告包裹。

PM> Install-Package Nager.AmazonProductAdvertising

控制器示例

public ActionResult ProductSearch(string search)
{
    var authentication = new AmazonAuthentication();
    authentication.AccessKey = "accesskey";
    authentication.SecretKey = "secretkey";

    var wrapper = new AmazonWrapper(authentication, AmazonEndpoint.DE);
    var result = wrapper.Search(search);

    return View(result);
}

示例视图

@model Nager.AmazonProductAdvertising.Model.AmazonItemResponse
@{
    ViewBag.Title = "Search";
}

<table class="table">
<tr>
    <th>ASIN</th>
    <th>SalesRank</th>
</tr>
@foreach (var item in Model.Items.Item)
{
    <tr>
    <td>@item.ASIN</td>
    <td>@item.SalesRank</td>
    </tr>
}
</table>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有更有效的方法来处理 C# ASP.NET(尤其是 MVC 5)上的亚马逊产品广告 API? 的相关文章

随机推荐