How to return boolean value from ASP NET Web API
1.Create an ASP.NET Web Application with WebForm2.Add Web API Project in the solution
3.Open the ValuesController in Controllers folder
4.Paste the below method
[HttpGet]
public bool GetValidateID(string id)
{
Boolean IsValid=true;
//Write your code to interact with DB Server
//IsValid = DAL.ValidateId(id);
return IsValid;
}
and make changes in WebApiConfig.cs file uunder App_Start folder
routeTemplate: "api/{controller}/{action}/{id}",
4.Go to Asp.net wenapplication
5.Open the webform (LogIn.aspx page in my case) and add one input control and a Button control
6.On Button Click call the "AuthenticateUser" in my case
7.Add the following key in the Asp.Net web form application config like under <configuration>
for the ServiceURL
<appSettings>
<add key="WebAPIServiceURL" value="http://localhost:49387/" />
</appSettings>
7.Call a method to send a get request to WebAPI for User validation, and use my like with little changes if required
public bool ValidateId(string UserId)
{
HttpClient WebAPIServiceClient = null;
Boolean IsValid;
HttpResponseMessage response;
string jsonString = string.Empty;
#region Consume GET method of webAPI
using (WebAPIServiceClient = new HttpClient())
{
WebAPIServiceClient.BaseAddress = new Uri(ConfigurationManager.AppSettings["WebAPIServiceUrl"].ToString());
WebAPIServiceClient.DefaultRequestHeaders.Accept.Clear();
WebAPIServiceClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
response = WebAPIServiceClient.GetAsync("api/values/GetValidateID?id=" + UserId).Result;
if (response.IsSuccessStatusCode)
{
jsonString = response.Content.ReadAsStringAsync().Result;
}
else
{
Message = "Internal server Error with API";
}
}
#endregion
return bool.TryParse(jsonString, out IsValid);
}
8.Include the required namespaces if any error
9.Before running the application, set both the project as startup project.
No comments:
Post a Comment
Your feedback is important.
Visit www.techwebdots.in