将参数添加到您的方法。您还需要WebInvoke上的一些其他属性。
这是一个示例(从内存来看,可能会有些偏离)
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "modifyMyPerson")]public void Modify(Person person) { ...}
对于人员类,如下所示:
[DataContract]public class Person {[DataMember(Order = 0)]public string FirstName { get; set; }}
和json这样发送
var person = {FirstName: "Anthony"};var jsonString = JSON.stringify({person: person});// Then send this string in post using whatever, I personally use jQuery
编辑:这是使用“包装”的方法。如果没有包装方法,您将取出
BodyStyle =...并对其进行字符串化,就可以了
JSON.stringify(person)。如果需要添加其他参数,我通常只使用包装方法。
编辑完整代码示例
Global.asax
using System;using System.ServiceModel.Activation;using System.Web;using System.Web.Routing;namespace MyNamespace{ public class Global : HttpApplication { protected void Application_Start(object sender, EventArgs e) { RouteTable.Routes.Add(new ServiceRoute("myservice", new WebServiceHostFactory(), typeof(MyService))); } }}
Service.cs
using System;using System.ServiceModel;using System.ServiceModel.Activation;using System.ServiceModel.Web;namespace MyNamespace{ [ServiceContract] [ServiceBehavior(MaxItemsInObjectGraph = int.MaxValue)] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class MyService { [OperationContract] [WebInvoke(UriTemplate = "addObject", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] public void AddObject(MyObject myObject) { // ... } [OperationContract] [WebInvoke(UriTemplate = "updateObject", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] public void UpdateObject(MyObject myObject) { // ... } [OperationContract] [WebInvoke(UriTemplate = "deleteObject", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] public void DeleteObject(Guid myObjectId) { // ... } }}
并将其添加到
Web.config
<system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> </system.serviceModel>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)