I have created a web api and I want to consume it in gridview of webform. But it needs key of the api. Can anyone plz tell me how can i find it?
Loading
I have created a web api and I want to consume it in gridview of webform. But it needs key of the api. Can anyone plz tell me how can i find it?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Anoop Kumar SharmaRank #127 · 17.4K reputationPosted Apr 19, 2022, 3:00 AM EDT
Krutika KarekarRank #4,938 · 318 reputationPosted Apr 20, 2022, 5:11 AM EDT
Krutika KarekarRank #4,938 · 318 reputationPosted Apr 18, 2022, 3:24 AM EDT
Krutika KarekarRank #4,938 · 318 reputationPosted Apr 18, 2022, 4:34 AM EDT
Krutika KarekarRank #4,938 · 318 reputationPosted Apr 18, 2022, 4:36 AM EDT
Model part
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime JoiningDate { get; set; }
public int Age { get; set; }
}
Controller part
public class EmployeesController : ApiController GetAllEmployees()
{
Employee[] employees = new Employee[]{
new Employee { ID = 1, Name = "Mark", JoiningDate =
DateTime.Parse(DateTime.Today.ToString()), Age = 30 },
new Employee { ID = 2, Name = "Allan", JoiningDate =
DateTime.Parse(DateTime.Today.ToString()), Age = 35 },
new Employee { ID = 3, Name = "Johny", JoiningDate =
DateTime.Parse(DateTime.Today.ToString()), Age = 21 }
};
public IEnumerable
{
return employees;
}
public IHttpActionResult GetEmployee(int id)
{
var employee = employees.FirstOrDefault((p) => p.ID == id);
if (employee == null)
{
return NotFound();
}
return Ok(employee);
}
}
Here I am populating grid using Api data
public void populateWebApiGridView()>().Result;
{
string URL = "http://localhost:34613/api/Employee";
string urlParameters = "API_KEY"; ///////Here I need the key
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("JoiningDate", typeof(DateTime));
dt.Columns.Add("Age", typeof(int));
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(URL);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync(urlParameters).Result;
if (response.IsSuccessStatusCode)
{
var dataObjects = response.Content.ReadAsAsync
foreach (var d in dataObjects)
{
dt.Rows.Add(d.ID);
dt.Rows.Add(d.Name);
dt.Rows.Add(d.JoiningDate);
dt.Rows.Add(d.Age);
}
}
else
{
System.Diagnostics.Debug.WriteLine("{ 0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
client.Dispose();
grdEmployee.DataSource = dt;
grdEmployee.DataBind();
}
Uttam KumarRank #1,076 · 1.8K reputationPosted Apr 18, 2022, 4:45 AM EDT
Krutika KarekarRank #4,938 · 318 reputationPosted Apr 18, 2022, 5:37 AM EDT
Uttam KumarRank #1,076 · 1.8K reputationPosted Apr 18, 2022, 6:28 AM EDT
Krutika KarekarRank #4,938 · 318 reputationPosted Apr 18, 2022, 6:38 AM EDT
Uttam KumarRank #1,076 · 1.8K reputationPosted Apr 18, 2022, 8:12 AM EDT
Krutika KarekarRank #4,938 · 318 reputationPosted Apr 21, 2022, 6:41 AM EDT
Anoop Kumar SharmaRank #127 · 17.4K reputationPosted Apr 18, 2022, 3:01 AM EDT
Uttam KumarRank #1,076 · 1.8K reputationPosted Apr 18, 2022, 3:07 AM EDT
As you have mentioned you have created the API so I believe you must be aware about it fully. What do you mean by Key of the API?
In common practice we usually pass all the secured data in header of the API.
Please provide us the code and the issue you are encountering.