无法通过branch.io中的C#分配$ desktop_url和$ Marketing_title



我正在将branch.io请求复制到api的c#中,并试图通过newtonsoft再次序列化来创建URL。我已经很好地复制了URL创建请求的主要部分,并且确实会生成URL。但是当我尝试在查询中定义我的desktop_url和Marketing_title时,我有问题。

{"type":2, "branch_key":"key_test_lerbZ22zyjfpfFtl2auzukafywi220dN", "campaign":"new_product_annoucement", "channel":"email", "tags":["monday", "test123"], "data":"{"name": "Alex", "email": "alex@branch.io", "user_id": "12346", "$desktop_url": "https://www.google.com","$marketing_title": "This is Awesome"}"}

您可以从JSON查询中看到,桌面URL和营销标题都在前面有$符号,即$ desktop_url和$ Marketing_title。

我认为这是抑制我在C#中创建副本的问题。我知道查询是正确的,好像您现在运行了,它肯定会链接到Google的搜索页面。

我的C#代码如下:

   [HttpPost]
    public string GetWithBody([FromBody] getInfo info)
    {
        String mesh = info.affCode + "=========>" + info.appType;
        using (var client = new HttpClient())
        {
            var request = new branchIOinfo()
            {
                type = 2,
                branch_key = "key_test_lerbZ22zyjfpfFtl2auzukafywi220dN",
                campaign = info.appType,
                alias = info.affCode,
                data = new BranchRequestData
                {
                    desktop_url = "https://www.google.com/"
                }
            };
            var response = client.PostAsync("https://api.branch.io/v1/url",
                new StringContent(JsonConvert.SerializeObject(request).ToString(),
                    Encoding.UTF8, "application/json")).Result;
            if (response.IsSuccessStatusCode)
            {
                dynamic content = JsonConvert.DeserializeObject(
                    response.Content.ReadAsStringAsync()
                    .Result);
                return content.url;
            }
            else
            {
                return "Error Creating URL";
            }

我的C#模型如下:

namespace BranchIOAPI.Models
{
  public class getInfo
  {
     public string affCode { get; set; }
     public string appType { get; set; }
  }
public class branchIOinfo
{
    public int type { get; set; }
    public string branch_key { get; set; }
    public string campaign { get; set; }
    public string alias { get; set; }
    public BranchRequestData data { get; set; }
}
public class BranchRequestData
{
    public string desktop_url { get; set; }
}
}

我如何在此代码中复制该$符号,或者是什么是复制该json查询的正确方法。

当此提及分支时,问题实际上是关于在使用C#时如何更改属性名称的问题。

我相信,这里适当的方法是使用jsonpropertyattribute:http://www.newtonsoft.com/json/help/html/jsonpropertyname.htm

因此,在类定义中,您会做这样的事情:

public class BranchRequestData
{
    [JsonProperty("$desktop_url")]
    public string desktop_url { get; set; }
}

Alex来自分支:

一开始使用$的参数只是我们系统中的旧约约定。您可以像其他任何参数一样指定这些内容,因此,除非我忘记的C#有一些特殊的内容,否则只需使用$desktop_url$marketing_url作为data对象内的键都可以正常工作。

最新更新