I am new to wcf rest services. I have created two method like Welcome and Hello. The two methods are working working in the browser like http://localhost:14502/MyRestService.svc/Hello/xxx"
and http://localhost:14502/MyRestService.svc/Welcome. But when i was consuming the service in the asp.net application by using jquery. It gives null result. Please give resolve that.
[ServiceContract]
public interface IMyRestService
{
[OperationContract]
[WebGet( RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Welcome",BodyStyle=WebMessageBodyStyle.Wrapped)]
string Welcome();
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "Hello/{name}",BodyStyle=WebMessageBodyStyle.Wrapped)]
string Hello(string name);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Test?id={id}")]
int Test(int id);
}
public class MyRestService : IMyRestService
{
public string Hello(string name)
{
return "Hello " + name;
}
public int Test(int id)
{
return id + 1;
}
public string Welcome()
{
return "Welcome to Rest Services";
}
}
The web.config file is
="1.0"
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="RestService.MyRestService" behaviorConfiguration="ServiceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="RestService.IMyRestService" bindingConfiguration="webHttpBindingJsonP" behaviorConfiguration="web"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="123456" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="123456"/>
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingJsonP" crossDomainScriptAccessEnabled="true"/>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
The Asp.net web page is
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ConsumingWCFRestService.aspx.cs" Inherits="ConsumingWCFRestService" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnSubmit").click(function () {
var url = "http://localhost:14502/MyRestService.svc/Welcome";
$.getJSON(url, null, function (data) {
if (data != null) {
alert("success");
}
else {
alert(data);
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="submit" id="btnSubmit" value="Call Rest" />
</div>
</form>
</body>
</html>
Please anybody resolve and reply to me...