写既URI和Da​​taContract类参数的Web服务方法 [英] Write a webservice method with both URI and DataContract class parameters

查看:388
本文介绍了写既URI和Da​​taContract类参数的Web服务方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

?我

如何获得Web服务的方法来访问的URI和复杂的POST数据参数



我有在URI中某些参数的Web服务方法:

 / API / {事} / {ID}

我也有标有DataContract类/成员属性我想要的web服务的方法来接受。请注意,我的类包含一个子类,还标有DataContract /会员属性。

  [DataContract] 
公共类MOREDATA {
[数据成员]
公共字符串SomeString {搞定;组; }

[数据成员]
公共SubClass中的子数据{搞定;设置;}
}



我的方法声明如下:

  [OperationContract的] 
[WebInvoke(方法=POST,UriTemplate =/ API / {事} / {ID})]
酒店的公共MyWebSvcRetObj更新(串东西,字符串ID)

我将如何得到我的更新方法还接受MOREDATA类?我已经能够写出这样的方法:

  [OperationContract的] 
[WebInvoke(方法=POST) ]
公共MyWebSvcRetObj更新(MOREDATA IncomingData)

但只有当URI没有指定任何参数和URI看起来像

 API /更新

最后,我希望我的用户能够是这样的:

  $阿贾克斯({
网址:本地主机/网站/ API /事/ 12345
型:POST
数据:{
SomeString:我是一个字符串'
子数据:{
//...more数据
}
}
});



我试图把一个流作为更新方法的最后一个参数,但是这似乎并没有为有像我给的例子子类的工作。它最终有像子数据[SomeSubDataProp]键/值对:瓦尔。这似乎是我错了。我也尝试添加一个MOREDATA参数作为更新方法的最终参数,但我得到了说,它期待一个流(因为它的预期原始数据)错误。正是我试图做可能吗?或许我会对此错误的方法吗?



更新



由于Faizan Mubasher写道,我的用户将能够发送POST数据如果我的方法是这样的:

  [OperationContract的] 
〔WebInvoke(方法=POST,
UriTemplate =/ V1 / {}东西/(编号),
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare

[描述(更新)]
公共无效更新(MOREDATA传入,串什么的,字符串ID)

通过客户端看起来像:

  $阿贾克斯({
型:POST,
网址:的http://本地主机/应用/ API /事/ 12345',
的contentType:应用/ JSON',//需要指定类型
数据:JSON.stringify({/​​/注意字符串化的号召
SomeString:你好,世界,
亚纲:{
SUBSTR:'你好,再次',
SubInt:98765
}
}
});


解决方案

您想保持相同的 URI 获取发表




  1. 获取方法被调用带参数的 / API / {事} / {ID} 的,然后根据 ID ,将数据返回给客户端,他/她可以编辑。


  2. 在编辑完成后,发送编辑的数据传回服务器中使用发表法相同的 URI 的和的标识的如获取请求。




如果提到就这样,那么在这里是解决方案:



要做到这一点,创建两个方法有相同的 UriTemplate ,但名称不同。

  [的ServiceContract] 
公共接口IMyWebSvc
{

[OperationContract的]
〔WebGet(UriTemplate =/ API / {}东西/(编号),
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
MyWebSvcRetObj的GetData(字符串的东西,串ID);

[OperationContract的]
[WebInvoke(方法=POST,UriTemplate =/ API / {}东西/(编号),
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
串更新(MOREDATA IncomingData,串什么的,字符串ID);
}

现在,什么应该是JSON格式。这很容易。在你的 MOREDATA 类,替换的ToString()方法。

 公共重写字符串的ToString()
{
的JavaScriptSerializer JS =新的JavaScriptSerializer();
返回js.Serialize(本);
}



的JavaScriptSerializer 是在 System.Web.Script.Serialization 可用。现在,检查哪个JSON格式您的服务将接受,做一个虚拟调用Web服务的方法和 object.ToString创下了破发点()方法。

  MOREDATA MD =新MOREDATA(); 
串jsonString = md.ToString(); //这里打破发点。

当你打破发点,在 jsonString 变量,你将有JSON,你的服务能够接受。所以,建议你的客户在该格式发送JSON。



所以,你可以在你的 MOREDATA 类。



我希望这会帮助你!谢谢


How can I get a web service method to access parameters in the URI and complex POST data?

I have a web service method with some parameters in the URI:

"/api/{something}/{id}"

I also have a class marked with DataContract/Member attributes that I want the webservice method to accept. Note that my class contains a subclass also marked with DataContract/Member attributes.

[DataContract]
public class MoreData{
    [DataMember]
    public string SomeString { get; set; }

    [DataMember]
    public SubClass SubData {get; set;}
}

My method declaration looks like:

[OperationContract]
[WebInvoke( Method = "POST", UriTemplate = "/api/{something}/{id}")]
public MyWebSvcRetObj Update(string something, string id)

How would I get my Update method to also accept the MoreData class? I've been able to write methods like:

[OperationContract]
[WebInvoke( Method = "POST")]
public MyWebSvcRetObj Update(MoreData IncomingData)

but only if the URI did not specify any parameters, and the URI would look like

"api/Update"

Ultimately I want my users to be able to something like:

$.ajax({
    url: 'localhost/site/api/things/12345
    type: 'POST'
    data: {
        SomeString: 'I am a string'
        SubData: {
           //...more data
        }
    }
});

I have tried putting a Stream as the final parameter in the Update method, but that doesn't seem to work for classes that have subclasses like the example I gave. It ends up having key/value pairs like "SubData[SomeSubDataProp]" : "Val". That seems wrong to me. I've also tried adding a MoreData parameter as the final param in the Update method, but I get an error that says it was expecting a stream (because it's expecting raw data). Is what I am trying to do possible? Or perhaps I am going about this the wrong way?

UPDATE

As Faizan Mubasher wrote, my users will be able to send post data if my web method looks like:

[OperationContract]
    [WebInvoke( Method = "POST", 
        UriTemplate = "/v1/{something}/{id}",
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare
    )]
    [Description( "Update " )]
    public void Update( MoreData Incoming, string something, string id )

With the client looking like:

$.ajax({
    type: 'POST',
    url: 'http://localhost/app/api/something/12345',
    contentType: 'application/json',                  //need to specify type
    data: JSON.stringify({                            //note the "stringify" call
        SomeString: 'hello, world',
        SubClass: {
            SubStr: 'hello, again',
            SubInt: 98765
        }
    }
});

解决方案

You want to keep same URI for both, GET and POST.

  1. When a GET method is called with parameters /api/{something}/{id}, then based on the id, it will return data to client that he/she can edit.

  2. When editing is done, send that edited data back to server using POST method with same URI and Id as mentioned in GET request.

If it is so, then here is the solution:

To do so, create two methods with same UriTemplate but with different names.

[ServiceContract]
public interface IMyWebSvc
{

    [OperationContract]
    [WebGet(UriTemplate = "/api/{something}/{id}",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare)]
    MyWebSvcRetObj GetData(string something, string id);

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/api/{something}/{id}",
        RequestFormat = WebMessageFormat.Json,    
        BodyStyle = WebMessageBodyStyle.Bare)]
    string Update(MoreData IncomingData, string something, string id);       
}

Now, what should be the format of JSON. It is easy. In your MoreData class, override the ToString() method.

public override string ToString()
{
    JavaScriptSerializer js = new JavaScriptSerializer();
    return js.Serialize(this);
}

The JavaScriptSerializer is available in System.Web.Script.Serialization. Now, to check which JSON format your service will accept, make a dummy call to your web service method and hit a break point on object.ToString() method.

MoreData md = new MoreData();
string jsonString = md.ToString(); // hit break point here.

When you hit break point, in jsonString variable, you will have json that your service will accept. So, recommend your client to send JSON in that format.

So, you can specify as much as complex types in your MoreData class.

I hope this will help you! Thanks

这篇关于写既URI和Da​​taContract类参数的Web服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆