You can make your own API’s if you have your database setup and make your own applications based on your own services. These days many large organizations have different departments to setup API’s for programmer and then programmers uses those API’s to make applications. This is very unique and easy way to your own API’s i have worked a lot on Microsoft Azure , Making own services and also a lot of windows phone and windows application development. In all this years of my experience in development i have find this the most easiest and most simplest way to make API’s
Before all of this please make sure to enable firewall rules for your system so that you can do your testing
Make a New project ASP.NET Empty Web Application name it.
After your have created your project now add Newtonsoft (JSON.NET dll) reference to your project
Add class called connection.cs
Add following code to the connection.cs file
Here this connections string is your SQL Server database connection string
Add Another class named Classes.cs
and add following code to it:
public class Classes { public Classes() { ID = -1; Name = null; Age = null; } public Classes(int id, string name, string age) { ID = id; Name = name; Age = age; } public int ID { get; set; } public string Name { get; set; } public string Age { get; set; } } public class ClassesData { public List ListofData = new List(); }
Add this to WebForm.aspx.cs
ClassesData data = new ClassesData(); string key = null; protected void Page_Load(object sender, EventArgs e) { key = Request["key"]; if (key == "yourapikey") { Load(); } else { data.ListofData = null; Response.Write(JsonConvert.SerializeObject(data)); } } public void Load() { SqlConnection connection = new SqlConnection(Connection.ConnectionString); try { connection.Open(); SqlCommand readcommand = new SqlCommand("SELECT * FROM Sample", connection); SqlDataReader reader = readcommand.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { data.ListofData.Add(new Classes(Convert.ToInt32(reader["ID"].ToString()), reader["name"].ToString(), reader["age"].ToString())); } Response.Write(JsonConvert.SerializeObject(data)); } else { data.ListofData = null; Response.Write(JsonConvert.SerializeObject(data)); } reader.Close(); connection.Close(); } catch (Exception ex) { data.ListofData = null; Response.Write(JsonConvert.SerializeObject(data)); } }
Recent Comments