Reputation: 409
I have made a WCF service in .NET 4 platform, that when I hit it with a jquery ajax POST it returns JSON. The issue that I have is that I would prefer if the json response of the POST is not wrapped in the name of the Method with the Result suffix.
In detail:
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
Person GetInfo(string id);
}
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
public class Service: Iservice
{
public Person GetInfo(string id)
{
...
return new Person();
}
}
public class Person
{
public string FirstName;
public string LastName;
public Person(){
FirstName = "Jon";
LastName = "Doe";
}
}
web.config
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehavior" name="Service">
<endpoint address="" binding="webHttpBinding"
contract="IService" behaviorConfiguration="EndpBehavior"/>
</service>
</services>
</system.serviceModel>
jquery:
var myparameters = JSON.stringify({ id: $('#id').val()});
$.ajax({
type: "POST",
url: "/Service.svc/GetInfo",
data:myparameters,
contentType: "application/json",
dataType: "json",
success: function (response) {
...
}
}
});
With the BodyStyle = WebMessageBodyStyle.Wrapped
the response a get in my javascript code is:
{"GetInfoResult" : {"FirstName":"Jon", "LastName":"Doe"}}
But when I change it to WebMessageBodyStyle.Bare
and everything else remains the same, a 500 internal server error occurs.
Is it possible to return Bare json in my POST response without the Method+Result wrapper? If yes what am I doing wrong?
Thanks in advance
Upvotes: 1
Views: 2928
Reputation: 2551
I think that WebMessageBodyStyle.Bare
expects also a "bare" request, and you are sending an object from your javascript code, something like this : {id:'value'}
that will be translated as :
public class DTObject{
public string Id { get; set; }
}
And your operation method expects only a string
as parameter.
Try to make only your response "bare" like this WebMessageBodyStyle.WrappedRequest
.
[Edit] http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webmessagebodystyle.aspx
Upvotes: 3