Hi Jilsoft,
Using the article i have created the example.
Model
Public Class CustomerModel
Private newPropertyValue As String
Public Property Name As String
End Class
Web API Controller
Imports System.Net
Imports System.Web.Http
Public Class CustomerAPIController
Inherits ApiController
<Route("api/CustomerAPI/GetCustomers")>
<HttpPost>
Public Function GetCustomers(ByVal customer As CustomerModel) As List(Of Customer)
Dim entities As NorthwindEntities = New NorthwindEntities()
Return (From c In entities.Customers.Take(10)
Where c.ContactName.StartsWith(customer.Name) OrElse String.IsNullOrEmpty(customer.Name)
Select c).ToList()
End Function
End Class
Global.asax
Imports System.Web.Http
Imports System.Web.Optimization
Public Class WebApiApplication
Inherits System.Web.HttpApplication
Sub Application_Start()
AreaRegistration.RegisterAllAreas()
GlobalConfiguration.Configure(AddressOf WebApiConfig.Register)
RouteConfig.RegisterRoutes(RouteTable.Routes)
End Sub
End Class
HTML
Name:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="Search" />
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
<asp:BoundField DataField="City" HeaderText="City" />
</Columns>
</asp:GridView>
Namespaces
Imports System.Net
Imports System.Text
Imports System.Web.Script.Serialization
Code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.PopulateGridView()
End If
End Sub
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Me.PopulateGridView()
End Sub
Private Sub PopulateGridView()
Dim apiUrl As String = "http://localhost:52730/api/CustomerAPI"
Dim input As Object = New With {Key _
.Name = txtName.Text.Trim()
}
Dim inputJson As String = (New JavaScriptSerializer()).Serialize(input)
Dim client As WebClient = New WebClient()
client.Headers("Content-type") = "application/json"
client.Encoding = Encoding.UTF8
Dim json As String = client.UploadString(apiUrl & "/GetCustomers", inputJson)
gvCustomers.DataSource = (New JavaScriptSerializer()).Deserialize(Of List(Of Customer))(json)
gvCustomers.DataBind()
End Sub
Public Class Customer
Public Property CustomerID As String
Public Property ContactName As String
Public Property City As String
End Class
Screenshot