English 中文(简体)
页: 1
原标题:asp.net webservice return json type

(b) 使用作为工具的网络服务:

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<MyObject> GetList()
{
    ....return new List of MyObject{ x =  .., y = .. , z = ..};
}

使用这一服务的客户用JQuery Ajax电话进行良好工作。

$.ajax({
        type: "POST",
        url: url,
        data: data == null ? "{}" : data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
 ..... and so on ... 

但有点火,我注意到答复是:

{"d":[{"__type":"Common.MyObject","z":"2000","x":1500,"y":1000,"a":"0"},{"__type":"Common.MyObject","z":"2000","x":1455,"y":1199.57,"a":"1"} ...... and so on ]}

1) question is why do I need this ""__type":"Common.MyObject" ?
2) I want to remove it so the response will be smaller , how can I do it ?

最佳回答
问题回答

当我拥有像以下这样的网络服务时,它会给我带来罚款:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
class MyObjectService
{
    [OperationContract]
    [WebGet(UriTemplate = "MyObjects", ResponseFormat = WebMessageFormat.Json)]
    public IEnumerable<MyObect> GetAlMylObjects()
    {
        MyObjectMgr objectMgr = new MyObjectMgr();
        return objectMgr.GetAll();
    }

}

这里是MyObjectMgr的法典:

public class MyObjectMgr
{
    public List<MyObect> GetAll()
    {
        List<MyObect> objList = new List<MyObect>();
        objList.Add(new MyObect { x = 1, y = 21, z = 33 });
        objList.Add(new MyObect { x = 4, y = 51, z = 66 });
        return objList;

    }
}

这里的答复是:

[{"x":1,"y":21,"z":33},{"x":4,"y":51,"z":66}]

我正在使用GET而不是POST,但我认为这不会有任何改变。 通常,如果你只是要检索你想要使用GET的信息,则使用LEST的软件。

我停止了利用WCF进行无害环境技术的网络服务,因为它难以混淆和温和。 我开始使用。 ASP.NET WebPIC/a> 它是即将发行的4号MVC释放的一部分。 更方便地安装了具有教育意义的转机。 您无需具体说明您是否希望担任这一职务。 客户可在吉大港山区负责人中说明这一点,即应当如何工作。

对逾期答复的担忧! 我最近遇到了类似的问题。 我得以在不重写我的服务的情况下确定这一服务:

  • 访问您的网页。 简讯

  • 行为定位。

    In my case, behavior was called "Project1.Services.DataTableAspNetAjaxBehavior" and this was found under <system.serviceModel>
    <behaviors>
    <endpointBehaviors>

PICTURE: enter image description here

  • Finally, add <webHttp defaultOutgoingResponseFormat="Json" /> to your behavior.

(NOTE: 如果你想要保持一个包裹,则使用添加:
><webHttpaultBodyStyle=“Wrapped”缺OutongoResponseFormat=“Json”/>
OR <>><>>>>>>>code><webHttpailBodyStyle=“WrappedResponse”缺Out 继续ResponseFormat=“Json”/&>

我希望这一帮助! 欢乐!





相关问题
IIS 6.0 hangs when serving a web-service

I am having issues with one of our web-services. It works fine on my development machine (win XP) whether I host it as a separate application or using cassini from Visual studio. Once I deploy on the ...

ASP.net web services

I am using a web service which sets the Thread.CurrentPrincipal object while logging in and soon later when another webmethod of the same web service accesses Thread.CurrentPrincipal, its different/...

Unity Container Disposing and XML Web Service

I am registering some wrapers over un-managed objects in container. How can I dispose of them at the end of the container s lifetime? Please bear in mind I have an XML Web service.

SharePoint : web service permission error

I have a sharepoint site, and I am calling a standard sharepoint web service. I create the web service request like this : wsDws.Url = this.SiteAddress + @"/_vti_bin/Dws.asmx"; When I use ...

热门标签