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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Krutika KarekarPosted Apr 21, 2022, 10:41 AM
Krutika KarekarPosted Apr 20, 2022, 9:11 AM
Anoop Kumar SharmaPosted Apr 19, 2022, 7:00 AM
Uttam KumarPosted Apr 18, 2022, 12:12 PM
Krutika KarekarPosted Apr 18, 2022, 10:38 AM
Uttam KumarPosted Apr 18, 2022, 10:28 AM
Krutika KarekarPosted Apr 18, 2022, 9:37 AM
Uttam KumarPosted Apr 18, 2022, 8:45 AM
Krutika KarekarPosted Apr 18, 2022, 8:36 AM
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();
}
Krutika KarekarPosted Apr 18, 2022, 8:34 AM
Krutika KarekarPosted Apr 18, 2022, 7:24 AM
Uttam KumarPosted Apr 18, 2022, 7:07 AM
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.
Anoop Kumar SharmaPosted Apr 18, 2022, 7:01 AM