0

I have created an ASP.NET Web API which finds the sum of two integers using an addition function from a dll file. When i run the application (http://localhost:52241/api/dlladdition/9/6) i get results as "15". The result i want is something like { "result" : 15} (JSON format). How do i achieve that? The JSON codes that i have included in my codes does not seem to work.

Here are my codes:

Addition.cs

using ClassLibraryDll;
public class Addition
{
    static int num1;
    static int num2;
    int sum = MathClass.Add(num1, num2);
}

DllAdditionController.cs

public class Temp
{
    public int num1 { get; set; }
    public int num2 { get; set; }
    public int sum { get; set; }
}

public class DllAdditionController : ApiController
{
    private Addition addition = new Addition();

    public int GET(int num1, int num2)
    {
        int result = ClassLibraryDll.MathClass.Add(num1, num2);
        string json = JsonConvert.SerializeObject(result);
        return (json);
    }
}

Someone please help me. Thank you so much in advance.

Susmitha Naidu
  • 109
  • 1
  • 12
  • Please show the code which is making a call to the API – Souvik Ghosh Nov 09 '17 at 01:54
  • Possible duplicate of [How do I get ASP.NET Web API to return JSON instead of XML using Chrome?](https://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome) – Souvik Ghosh Nov 09 '17 at 01:58
  • This whole application is the API that i am creating. It calls a dll file (using `ClassLibraryDll` i have referenced the dll file) – Susmitha Naidu Nov 09 '17 at 01:58
  • Are you trying to run http://localhost:52241/api/dlladdition/9/6 using a Web browser? – Dan Nguyen Nov 09 '17 at 02:07
  • yeS i am trying to run that using a web browser – Susmitha Naidu Nov 09 '17 at 02:08
  • 1
    `{15}` this is not a valid JSON, valid JSON object always has pairs of key (property quoted by "") and value. If you want some valid JSON object in this case, it should be `{ "result" : 15}` or simply `15`. – King King Nov 09 '17 at 02:48
  • @KingKing I want to get `{ "result" : 15}` – Susmitha Naidu Nov 09 '17 at 02:50
  • How do i get that result? – Susmitha Naidu Nov 09 '17 at 02:50
  • 1
    that format requires an object, you can declare a class having one property of `result` (whose data Type is `int`). Well it's a bit more code to write. Your `GET` method of course should return that type, not `int`. – King King Nov 09 '17 at 02:52
  • 1
    You're trying to complicate things, the simple return value of `15` should be totally fine in this case. – King King Nov 09 '17 at 02:54
  • @KingKing my project manager insists for it to be in JSON format :/ – Susmitha Naidu Nov 09 '17 at 04:26
  • 1
    unless you design your own of JSON result format with complex object. The simple value `15` is also a valid JSON-parsable string, meaning deserializing `"15"` would return the number 15. – King King Nov 09 '17 at 06:01

2 Answers2

1

You can set the response to by JSON by using the JSON Media-Type Formatter

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.UseDataContractJsonSerializer = true;

If you like to restrict your Web API response to either JSON or XML only, you can call this from your Application_Start method, defined in Global.asax.

void ConfigureApi(HttpConfiguration config)
{
    // Remove the JSON formatter
    config.Formatters.Remove(config.Formatters.JsonFormatter);

    // or

   // Remove the XML formatter
   config.Formatters.Remove(config.Formatters.XmlFormatter);
}

You may refer to https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization#removing-the-json-or-xml-formatter for more information.

j.f.
  • 184
  • 9
  • Where do i put `var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.UseDataContractJsonSerializer = true;` this set of codes? – Susmitha Naidu Nov 09 '17 at 02:25
  • 1
    Hi, can put it inside Global.asax.cs file, inside the Application_Start(). Or if you are using any other startup class, just add the line of codes in. – j.f. Nov 09 '17 at 02:44
1

my project manager insists for it to be in JSON format :/

It is valid JSON. Your project manager needs to read up on the current JSON specs.

If you want your result wrapped in an object with a result property, you are free to define a class with a single property result and return this class instead.

Heinzi
  • 167,459
  • 57
  • 363
  • 519