How to consume Asp.Net Web API using C# ?


Asp.Net web api can be consumed in different ways in asp .net web applications. Be it Javascript, jQuery or using C#.

This post will illustrate how to consume it via C# code.


ASP.NET Web API:  It is a framework which can easily build HTTP services can be accessible by clients, including browsers and mobile devices. Its an ideal platform for building RESTful applications on the .NET Framework.

Simple Code Snippet of Web API are given below.

public class ResponseType
public string Status { get; set; }
public string Token { get; set; }

public HttpResponseMessage GetGlobalToken(string AutorizationKey)
HttpResponseMessage response = new HttpResponseMessage();
ResponseType objResponse = new ResponseType();
APIMethods objApiMethods = new APIMethods();

int intIsValidAuthKey = objApiMethods.IsValidAuthKey(AuthorizationKey);

//Validates the authorization key
if (intIsValidAuthKey == 1)
string strGlobalToken = ReturnToken();
objApiMethods.UpdateClientToken(dto.AuthorizationKey, strGlobalToken);

objResponse.Status = "Success";
objResponse.Token = strGlobalToken;

objResponse.Status = "Error";
objResponse.Token = "200";

response = Request.CreateResponse(HttpStatusCode.OK, objResponse, new MediaTypeHeaderValue(GetContentType));
return response;
catch (Exception ex)
throw ex;

In the above code, GetGlobalToken() is a web api method which gets Authorization Key as a parameter.
It matches the Authorization Key by passing it to database and if the key matches then it returns the response containing Status as well as Token.

Now we will consume this web api via C# code.
C# code snippet is given below.

private void DemoToConsumeApi()
string strAuthKey = string.Empty;

// Calls the web service by passing the parameters
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost/TestSite/DesktopModules/TestAPI/API/TestServices/");
new MediaTypeWithQualityHeaderValue("application/xml"));

HttpResponseMessage response = client.GetAsync("GetGlobalToken?AuthorizationKey=" + strSignUpGlobalToken + "").Result;

// If response comes as success, then read the response
if (response.IsSuccessStatusCode)
var res = response.Content.ReadAsStringAsync().Result;

XDocument doc = XDocument.Parse(res);
XmlDocument xdoc = new XmlDocument();

XmlNodeList status = xdoc.GetElementsByTagName("Status");
XmlNodeList token = xdoc.GetElementsByTagName("Token");

strMessage = status[0].InnerText;
strToken = token[0].InnerText;

if(strMessage == "Success")
// Do operation
// See for error code in response

In the above method we are using the path of web api method as
because method is inside the DesktopModules of TestSite.


In this way we can easily consume the web api using C# code.



Software Engineer | Mindfire Solutions

Tagged with: , , , , , , , , , ,
Posted in DotNetNuke
One comment on “How to consume Asp.Net Web API using C# ?
  1. […] How to consume Asp.Net Web API using C# ? ( […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


Enter your email address to follow this blog and receive notifications of new posts by email.

Join 10 other followers

%d bloggers like this: