Web api practice session C# from scratch : tutorial link
http://www.tutorialsteacher.com/webapi/web-api-tutorials
namespace WebAPI.Configuration
{
public static class HelloWebAPIConfig
{
public static void Register(HttpConfiguration conf)
{
conf.MapHttpAttributeRoutes();
conf.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "ash/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
Global.asax
protected void
Application_Start(object sender, EventArgs e)
{
GlobalConfiguration.Configure(HelloWebAPIConfig.Register);
}
Controller code
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace WebAPI.Controller
{
//[Authorize()]
public class HelloController : ApiController
{
//// tutorial
step 1 ////
//public string
Get()
//{
// return "Hello World";
//}
//// tutorial
step 2 ////
//[HttpGet]
//public string
TestAshwini123()
//{
// return "Hello World";
//}
//// tutorial
step 3 ////
http://localhost:59736/ash/hello?id=5
//[HttpGet]
//public string TestAshwini123(int id)
//{
// return "Hello World" + id;
//}
//// tutorial
step 4 ////
http://localhost:59736/ash/hello?id=5&name=Ashwini
//[HttpGet]
//public string
TestAshwini123(int id, string name)
//{
// return "Hello " + name + "
id:" + id;
//}
//// tutorial
step 5 ////
//[HttpPost]
//public string
TestAshwini123([FromBody] Student s1)
//{
// return "Hello " + s1.Name +
" id:" + s1.Id;
//}
//// tutorial
step 6 ////
[HttpPost]
public string TestAshwini123([FromBody] Student s1)
{
s1.Name = User.Identity.Name; //"Hello " + s1.Name;
DataSet ds=DbCon.GetResultFromSqlDatabase("Select
* from dbo.AngularEmployee", new List<SqlParameter>());
s1.Id = 1 + s1.Id;
return JsonConvert.SerializeObject(ds.Tables[0]);
}
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
public class DbCon
{
public static DataSet GetResultFromSqlDatabase(string cmdText, List<SqlParameter> paramColl)
{
string DBConnectionString = string.Empty;
if (ConfigurationManager.ConnectionStrings["SQLConnectionString"] != null)
DBConnectionString = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ToString();
SqlDataAdapter sqlAdapter = null;
SqlConnection sqlConnection = null;
SqlCommand sqlCommand = null;
DataSet dsResult = new DataSet();
if (!String.IsNullOrEmpty(DBConnectionString))
{
sqlConnection = new SqlConnection(DBConnectionString);
sqlCommand = new SqlCommand(cmdText, sqlConnection);
//sqlCommand.CommandType
= CommandType.StoredProcedure;
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SqlConnectionTimeout"]))
sqlCommand.CommandTimeout =
Convert.ToInt32(ConfigurationManager.AppSettings["SqlConnectionTimeout"]);
foreach (SqlParameter p in paramColl)
sqlCommand.Parameters.Add(p);
sqlAdapter = new SqlDataAdapter(sqlCommand);
sqlAdapter.Fill(dsResult);
}
sqlAdapter.Dispose();
sqlConnection.Dispose();
sqlCommand.Dispose();
DBConnectionString = string.Empty;
return dsResult;
}
}
}