How to use WebClient in C# (System.Net.WebClient)

System.Net.WebClient
Using System.Net.WebClient class in consuming Web API.

System.Net.WebClient – The easiest way to download and upload data from and to a Web API is using Web Client. Here’s a few codes  to implement Web Client in 4 different method.

  • using System.Net;

Method: GET

Method: POST

Method: DELETE

Method: PUT


 

Restful Web Services C# – ASP.NET Web API

ASP.NET Web API – In my previous blog ASP.NET Web Application using MVC I have created a simple project that explains how to create Restful Web Services(WEB API) using MVC default route. By this time I will create another project that will show you how to create ASP.NET API using API Controller class. This is the standard flow on creating API project in ASP.NET.

Let’s Start:

Step 1. First, open your visual studio and create new Project.

Restful Web Services

New Project

Step 2. Name your Project to your preference. In my case I name it as myRestAPI. Then press “OK”.

Restful Web Services

Name Project

Step 3. Select Empty from your template window shown below and check Web API from the checkbox below template option.

Restful Web Services

Select Empty Template

This is how your solution explorer looks for an Empty template project

Restful Web Services

Solution explorer

Step 4. Now add Web API Controller class inside your Controllers folder. To add right click on your Controller folder, select Add then choose New Item.

Restful Web Services

Add Web API Controller Class

Step 5. From the New Item window search for Web API Controller Class(v2) . Name your class controller then press Add.

Restful Web Services

Name Web API Controller

The image below is the API Controller Default route:

Restful Web Services

Web API URL Default Route

Step 6. Before we proceed with this file (MyAPController) let’s create an additional class inside Model folder. This will be used as a model to create our sample data later. To add, right click on your Model folder select Add then choose New Item.

Restful Web Services

Add Model Class

Step 7. Name your model. In my case, I name it as myModel.

Restful Web Services

Name Your Model

Step 8. Open myModel class then declare properties below.

Step 9. Open MyAPIController inside your Controller folder. If you name your API Controller same as mine you can copy and replace your code with the code below. If not manually create the method from the code below.

Method:

  • GetList() >> Return all data we created with our model.
  • GetName(int id) >> Return specific data by id.
  • Model >> Dummy list we create base on our myModel class.

Solution Explorer Overview – Restful Web Services

  • WebApiConfig >> contains default url route for Api Controller
  • myAPIController >> Api controller that contains our method
  • myModel >> Contains model data properties.
Restful Web Services

Solution Explorer

Step 10. To access GetList(). Run your project and navigate to this URL localhost:7831/api/myAPI

Restful Web Services

GetList()

Step 11. To access GetName(int id) >> localhost:7831/api/myAPI/GetName?id=1

Restful Web Services

GetName()

 

Code Summary:

MyAPIController

myModel:

From this moment you have now your own ASP.NET Web API. Thank you for reading.

Happy coding!!

 


Post you may like:

[pt_view id=”2384ae03it”]

Restful Web Services Tutorial with ASP.NET MVC

Restful Web Services Tutorial

Restful Web Services Tutorial – From my previous Article on how to Create Web API in ASP.NET. We have created an API method that will return Hello + the name input by the user. Now in this article let’s consume that API using a client Application.

Let’s start: – Restful Web Services Tutorial

1. Create a new ASP.NET MVC Web Application Project.

Create New Project

2. I assume you have already created a project. Now we will use again the default Controller of MVC which is HomeController. Open HomeController by navigating to Solution explorer->Controller folder and open HomeController.

HomeController

3. From your Index function inside your HomeController, lets create additional code that will connect as to the API we created from the previous blogs ASP.NET WEB API using ASP.NET MVC.

Add below Code to your index.

  • using System.Net » For Webclient(). Which is use to connect to our Web API
  • using System.Data » For DataTable()
  • using Newtonsoft.Json » JSON
  • Viewbag.Name = response » dynamically share values from our controller to our view.
  • John » Parameter value. It could be anything you want to supply.

 

Code:

The image below is the screenshot of my HomeController code.

Home Controller Code

4. Now to display this in our view and to make it as simple as possible I use ViewBag Property to pass data to our view. Now navigate to your View (index.cshtml) under Views->Home folder and open index.cshtml.

Index.cshtml

5. Replace the code from your actual index.cshtml using the code shown in the image below. This display whatever we save to our ViewBag.Name Property.

Index.cshtml

6. Lastly, Run your project using f5 to run your project in debugging mode or CTRL + f5 to run project without debugging mode. This should output parameter name we declare in this code from our index() method in HomeController.

  • string url = “http://localhost:41480/Home/HelloWorld?name=” + “John”;

If we replace the name parameter John into Regie from the code above the output should now be Hello Regie. But because I use John in my code the image below shows Hello John output.

 

Note:  Because we are using local project you need to run both API and client application to access both project. You can also publish your API in your local IIS read this thread How to Host ASP.NET Web App in IIS.

 

Output

That’s all we need in consuming Web API in ASP.NET.

Comments are greatly appreciated to improve my blog writing technique.

Thank you for reading. Happy Coding!

You might to want to revisit this blog  Create Web API in ASP.NET.


Post you may like:

[pt_view id=”2384ae03it”]

Rest API Application – ASP.NET MVC

Rest API Application – ASP.NET WEB API is a framework that allows you to build an HTTP service. This service can be accessed online using HTTP requests. API’s can include all possible method that exposed data from a database. Like INSERT, SELECT, DELETE and UPDATE. And because API is an HTTP service its method is accessible using a URL.

Let’s take a look at a simple example below on the actual way on how the API is created and behave in .NET framework.

Let’s Start: – Rest API Application

Step 1. Open your visual studio and go to File->New->Project. A New  Project dialog window will open.

Rest API Application

Create new Project

Step 2. From the left pane of New Project window select Templates->Visual C# -> Web. Then from the middle pane select ASP.NET Web Application. And name your application, in my case I name it as SampleAPI. Click “OK” if you’re done.

Rest API Application

New Project

Rest API Application

Step 3. Select a template. To make it simple we will use Web API with default MVC template. Choose Web API template and check MVC and Web API from checkbox right below you’re template option. See the image below.

Rest API Application

Templates

Because we are using MVC template for this project your start page will look like a standard ASP.NET MVC Web Application. But we are not really after the view in this project. Method inside API’s is access directly using a URL. See image below.

Rest API Application

Start View

The image below is the default Folder structure created in MVC Framework.

Rest API Application

Folder Structure

 

Step 4. Now, after you have created a Web API project navigate to your solution explorer and open HomeController inside Controller folder. As I discussed from my previous blogs about MVC, HomeController is the default controller of MVC template in ASP.NET Web Application.

Rest API Application

HomeController

Step 5. For simplicity and better understanding of this article we will create a simple method that will return name input by a user.

Ex. John => Hello John 

I name the method HelloWorld() but instead of returning Hello world we will return Hello + the name input by the user. See image below for the code.

Rest API Application

Hello World

Step 6. To access the method we just created run your project using f5 command for debugging mode.  And use the URL shown in the image below to access the method.

  • URL format localhost:port#/ControllerName/MethodName?Parameter=value.
  • Value => Supplied in the client side application

If the user input Regie as the name parameter the output would be Hello Regie

  • localhost:41480/Home/HelloWorld?name=Regie

 

Rest API Application

Output

Rest API Application

This might be a simple example but this will help you start your own WEB API Project. You can create additional method like connecting to your database and return a set of list or Update data from your database. This is all possible if you just know the concept of API’s.

To consume this API using ASP.NET MVC visit this thread Consume Web API using ASP.NET MVC

Thank you for reading. Happy Coding!!


Post you may like:

[pt_view id=”2384ae03it”]

Basic PHP Restful Web Services Implementation(Web API)

Restful Web Services

API in Programming Overview:

          Application program interface (API) is a set of Instructions, protocol, or implementation that is created to be accessible in client side application. It is also used for securely transmitting of data when it comes to the database application. It Controls data flow and limit client side application to have a full control of the main data. A good API can secure database from being vulnerable to hacking.

Basic API Concept Diagram sample:

 

Restful Web Services

API Concept is to transmit data from a Database and used it in a Client side application like Websites or windows application through our Web API.

Instead of using actual database connection from a database, we are going to use an Array of data to represent our database and process data in Web API, and pass data to our client application which I’m going to use JQuery in receiving data from the API.

 

Coding Diagram:

  • Database.php => Contain Array of Data to represent our Database
  • myWebAPI.php => Represent as Web API
  • Consume.php => Represent as our Client side application that consumes our API

I use www.000webhost.com for my hosting . Click Here for free web hosting

I uploaded myWebAPI.php in 000WebHost and consume it using xampp Localhost in my local Computer.

Database.php

Note:
$find => Parameter need to be pass to select data from array

 

Array return data
• 1 = > Asp.Net
• 2 => PHP
• 3=> C#

 

If $find value is equal to 1 the return should be Asp.Net

 

myWebAPI.php

Consume.php

Note:
Common problem encountered in JQuery is the Cross-origin Resource Sharing (CORS) that is block in most of the hosting sites. As a work around we used JQuery JSONP datatype.

 

Ajax URL:

  • jsonCallback => is the  callback we specify in myWebAPI.php ($_GET[‘jsonCallback’].'(‘.$ json_response.’)’;)
  • number => is the parameter  from myWebAPI.php (($_GET[‘number’])))