im using WebApi to make a webservice
as follow:
- using RestSharp;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using Newtonsoft.Json;
-
- namespace test.Controllers
- {
- public class ValuesController : ApiController
- {
- public class WebAgent
- {
- public string Agent_no { get; set; }
- public string Agent_Name { get; set; }
- }
-
- public string GetAgent([FromBody] string a_body)
- {
- dynamic stuff = JsonConvert.DeserializeObject(a_body);
- string agent = stuff.agent_no.ToString();
- string name = stuff.agent_name.ToString();
- try
- {
- var test = new WebAgent()
- {
- Agent_no = agent,
- Agent_Name = name
- };
- JsonSerializerSettings settings = new JsonSerializerSettings
- {
- NullValueHandling = NullValueHandling.Ignore,
- Formatting = Formatting.Indented
- };
- var result= JsonConvert.SerializeObject(test,settings);
- return result;
-
- }
- catch (WebException e)
- {
- var message = e.Message;
- using (var reader = new StreamReader(e.Response.GetResponseStream()))
- {
- var content = reader.ReadToEnd();
- return content.ToString();
- }
- }
- finally
- {
-
- }
- }
- }
- }
and everything just working fine
but when i try to call my webapi service from postman i got the following response
- "{\r\n \"Agent_no\": \"122331\",\r\n \"Agent_Name\": \"sabatini\"\r\n}"
instead of:
- "{"Agent_no": "122331","Agent_Name": "sabatini"}"
i have tried to add
- config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings
- {
- NullValueHandling = NullValueHandling.Ignore,
- Formatting = Formatting.Indented
- };
to the WebApiConfig.cs but still get the same response !
i need help !
thanks