0

I am trying to send a JSON object to a webapi using aspnetcore. This seems a straightforward task. But, the fieldname of the JSON data includes a dash(-) e.g { Mj-TemplateID : 1}. Since Newtonsoft.Json is not available for .netcore, so I am not able to use JsonProperty to change the json fieldname like below.

public class SendData {
   [JsonProperty("Mj-TemplateID")]
   public string TemplateId { get; set; }
}

How can I specify a different name to be used while sending json data?

I have to send something like this

curl -s \
-X POST \
--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
https://api.mailjet.com/v3/send \
-H 'Content-Type: application/json' \
-d '{
    "FromEmail":"pilot@mailjet.com",
    "FromName":"Mailjet Pilot",
    "Subject":"Your email flight plan!",
    "MJ-TemplateID":"1",
    "MJ-TemplateLanguage":true,
    "Recipients":[
            {
                    "Email": "passenger@mailjet.com"
            }
    ]
}'
Set
  • 47,577
  • 22
  • 132
  • 150
dudedev
  • 451
  • 1
  • 5
  • 19

2 Answers2

1

Newtonsoft.json is compatible with asp.net core, please read: https://github.com/JamesNK/Newtonsoft.Json/issues/977

If you follow that ticket and the following one you can see the progress of it. A plausible fix is checking the "pre-release" button in nuget.

Jean-Paul
  • 380
  • 2
  • 9
  • 26
1

You already have dependency on Newtonsoft.json nuget in your project. Just try to add

using Newtonsoft.Json;

and JsonProperty attribute will be available for you.

Why you already have it? Your project has included reference to Microsoft.AspNetCore nuget package, that has Microsoft.Extensions.Configuration.Json package as the dependency, which, in its turn, uses the Newtonsoft.json. So after package restoring, all those packages are available for you.

Set
  • 47,577
  • 22
  • 132
  • 150
  • sorry, my bad. yes, it works. Visual Studio was not offering me ( as suggestion to fix error) to add "using" on compile error, then I checked the supported frameworks of Newtonsoft.Json and I did not found .netcore. So, I assumed that it does not work. – dudedev Apr 12 '17 at 10:59