1. Create Empty Web Project and add the following class along with its associated interface code
public class Person
{
public string Id
{
get;
set;
}
public string Name
{
get;
set;
}
}
[ServiceContract]
interface IPersonService
{
[OperationContract]
Person GetPO();
[OperationContract]
string SayHello(string name);
[OperationContract]
Person GetPerson(string id);
[OperationContract]
Person InsertPerson(Person person);
[OperationContract]
Person UpdatePerson(string id, Person person);
[OperationContract]
void DeletePerson(string id);
[OperationContract]
string SE();
[OperationContract]
Stream Post(string message, int x, int y);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PersonService : IPersonService
{
public List<Person> PersonList= new List<Person>();
[WebGet(UriTemplate = "SE")]
public string SE()
{
return "this is test";
}
[WebGet(UriTemplate = "ss")]
public Person GetPO()
{
Person p = new Person();
p.Id = "testid";
p.Name = "personname";
return p;
}
[WebGet(UriTemplate = "SayHello({name})")]
public string SayHello(string name)
{
return string.Format("Hello, {0}" , name);
}
[WebGet(UriTemplate = "Person({id})")]
public Person GetPerson(string id)
{
Person p = new Person();
p.Id = id;
p.Name = "person name";
return p;
}
[WebInvoke(UriTemplate = "Ins", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Person InsertPerson(Person person)
{
Person p = person;
p.Id = "changedid";
return p;
}
[WebInvoke(UriTemplate = "Person({id})", Method = "PUT")]
public Person UpdatePerson(string id, Person person)
{
var a = from p in ps
where p.Id == id
select p;
return a as Person;
}
[WebInvoke(UriTemplate = "Person({id})", Method = "DELETE")]
public void DeletePerson(string id)
{
ps.RemoveAt(int.Parse(id));
}
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, UriTemplate="")]
public Stream Post(string message, int x, int y) { return new MemoryStream(Encoding.UTF8.GetBytes("<b>" + message + (x + y) + "</b>")); }
}
Web.config :
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" minFreeMemoryPercentageToActivateService="1"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
minFreeMemoryPercentageToActivateService attribute is needed only when the red memory error occurs.
HTML Page:
function getPerson() {
var name = $("#name").val();
$.ajax({
cache: false,
type: "GET",
async: false,
url: "Person"+ "(" + name +")",
dataType: "json",
processData: false,
success: function (msg) {
alert(msg.Name);
},
error: function (err) {
//error handling
alert("Error Occured!" + err.msg);
}
});
}
function postdata() {
alert("begin");
$.ajax({ url: "/",
type: "POST",
contentType: "application/json",
data: '{"x":"1","y":"2","message":"The answer is: "}',
dataType: "html",
success: function (data) { $('body').html(data); },
error: function (err) { alert("Error Occured!" + err.msg);}
});
}
function InsertPeople() {
//調用方法並傳遞一個json格式的Person對象,返回結果是一個json格式對象
var data = { Name: "Denny", Id: "23" };
var jsonStr = JSON.stringify(data); //將對象格式化成json字串
sendAJAX("Ins", jsonStr,
function (msg) {
alert(msg.Name + " " + msg.Id + " this is a test case");
});
}
function sendAJAX(url, data, success) {
$.ajax({
type: "POST",
contentType: "application/json",
url: url,
data: data,
processData: false,
success: success,
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error Occured!");
}
});
}
The code above is the demo code for calling wcf rest service without svc file using jquery, hopefully it's a basic guideline for all of you:)