TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Omar Kh
NA
301
21.8k
How I can return json object in api controller?
Apr 18 2021 7:10 AM
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
Reply
Answers (
2
)
Set Value in Temp Data.
How to update data barang on table barang using trigger in sql server