No more nonsense, let’s get straight to the point
wcf end:
Restful has become more popular in recent years. In order to enable ajax calls and to support restful-style uri, after creating an Ajax-enabled Wcf Service, you must manually modify the svc file and specify the Factory, that is:
<%@ ServiceHost Language="C#" Debug="true" Service="ajaxSample.HelloWorld" CodeBehind="HelloWorld.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
Note: If Factory is not added, wcf will not be directly accessible using restful methods like http://localhost/helloWorld.svc/Hello/person/name.
At the same time, remove in web.config, which is similar to:
🎜>
" multipleSiteBindingsEnabled="true" /> binding="webHttpBinding" contract="ajaxSample.HelloWorld" />
Okay, let’s start writing code. Since there are two methods of GET/POST when calling wcf, let’s write an example method for several commonly used situations:
Copy the code
The code is as follows:
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
namespace ajaxSample
{
[ServiceContract(Namespace = "http://yjmyzz.cnblogs.com/")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class HelloWorld
{
///
/// 只能Post的Restful方法
/// ///
///
///
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "PostRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
public List
PostRestfulTest(string person,string welcome)
{
List result = new List();
result.Add("PostRestfulTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}
///
/// 只能Get的Restful方法
///
///
///
///
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GETRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
public List GETRestfulTest(string person, string welcome)
{
List result = new List();
result.Add("GETRestfulTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}
///
/// 即可Get与Post的Restful方法
///
///
///
///
[OperationContract]
[WebInvoke(Method = "*", UriTemplate = "RestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
public List RestfulTest(string person, string welcome)
{
List result = new List();
result.Add("RestfulTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}
///
/// 只能Post的常规方法(注:Post方式,BodyStyle必须设置成WrappedRequest或Wrapped)
///
///
///
///
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest)]
public List PostTest(string person, string welcome)
{
List result = new List();
result.Add("PostRestfulTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}
///
/// 只能Get的常规方法
///
///
///
///
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
public List GETTest(string person, string welcome)
{
List result = new List();
result.Add("GETTest -> from server:");
result.Add(person);
result.Add(welcome);
return result;
}
}
}
jQuery调用代码: