WCF Web API Json 格式
我是 MVC 中 WCFweb api WEb 服务的新手,我使用 ADO.net 实体框架做了一个示例服务,它以 XMl 格式返回结果,我想要 Json 格式,我编写了这样的代码。
[WebGet(UriTemplate = "ListAccount", ResponseFormat = WebMessageFormat.Json)]
public IEnumerable<account> Get()
{
IEnumerable<account> objAcct = from cat in objEntity.accounts select cat;
List<account> Result;
Result = new List<account>();
foreach (account Account in objAcct)
{
account objAcc = new account();
objAcc.AccountNumber = Account.AccountNumber;
objAcc.AccountType = Account.AccountType;
objAcc.BusinessName = Account.BusinessName;
objAcc.AccountId = Account.AccountId;
objAcc.PrimaryContactFirstName = Account.PrimaryContactFirstName;
objAcc.PrimaryContactLastName = Account.PrimaryContactLastName;
objAcc.PrimaryContactEmail = Account.PrimaryContactEmail;
objAcc.PrimaryContactPhone = Account.PrimaryContactPhone;
objAcc.AccountGuid = Account.AccountGuid;
Result.Add(objAcc);
}
return Result.AsQueryable();
}
请帮助我如何获得 Json 格式的结果?
I am new to WCFweb api WEb service in MVC I did a sample service using ADO.net Entity frame work it return result in XMl format I want to Json format I wrote the code like that.
[WebGet(UriTemplate = "ListAccount", ResponseFormat = WebMessageFormat.Json)]
public IEnumerable<account> Get()
{
IEnumerable<account> objAcct = from cat in objEntity.accounts select cat;
List<account> Result;
Result = new List<account>();
foreach (account Account in objAcct)
{
account objAcc = new account();
objAcc.AccountNumber = Account.AccountNumber;
objAcc.AccountType = Account.AccountType;
objAcc.BusinessName = Account.BusinessName;
objAcc.AccountId = Account.AccountId;
objAcc.PrimaryContactFirstName = Account.PrimaryContactFirstName;
objAcc.PrimaryContactLastName = Account.PrimaryContactLastName;
objAcc.PrimaryContactEmail = Account.PrimaryContactEmail;
objAcc.PrimaryContactPhone = Account.PrimaryContactPhone;
objAcc.AccountGuid = Account.AccountGuid;
Result.Add(objAcc);
}
return Result.AsQueryable();
}
Please help me how can i get Json format result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试发送带有值 application/json 的 Accept 标头。
Try sending a Accept header with the value application/json.