By Guillermo Cedillo
Use Microsoft Ajax to extend you services
If you want your service works as a REST service and return JSON is not necessary to do some complex modifications in the web.config or import special libraries. When you have a WCF or an asmx web service already implemented, the easiest and cleaner way to extend your services is using the ScriptManager Control of Microsoft Ajax.
First, enable your service to work with the Script Manager add or uncomment [ScriptService] in every web service that you want to extend.
Then in your page add the Script Manager and add the reference to your services like this:
<asp:ScriptManagerID="ScriptManager1"runat="server">
<Services>
<asp:ServiceReferencePath="~/WebService1.asmx"/>
Services>
asp:ScriptManager>
And that’s all you need. Now you can use your web services with javascript!
$.getJSON("WebService1.asmx/HelloWorld", function(data) {
alert(data.d);
});
Use ADO.NET Data Services
If you want an extreme REST service with complex queries, inserts, updates and deletes, there’s a very easy way to add this functionality in .NET.
First add the ADO.NET Entity Data Model to your project.
Then configure your Model. It is as easy as doing drag and drop from the tables in the data base. If you want more complex functions as implement store procedures or complex relations please visit this reference.
Then add a ADO.NET Data Service:
Then just do a relationship between your Model and the REST Service adding the Model class to the generic implementation of the DataService and set the access permissions for your tables.
After this the service has the ability to do complex operations in the query like:
/Person?$filter=id eq 1
/Person?$filter=id eq 10&$select=Name
And use the request verb to perform selects, inserts, deletes and updates as follow:
GET – select
POST – insert
PUT – update
DELETE – delete
For more information please visit http://msdn.microsoft.com/en-us/data/ee720180.aspx. Here you will find very nice tutorials and examples.
Return JsonResult in an ASP.NET MVC Controller
The last one is for ASP.NET MVC and it’s very easy! Just add JsonResult to the controller actions:
And that’s it!
I hope to see you again around the blog J
No comments:
Post a Comment