Web API using Stored Procedures and passed optional parameters :
Web API using Stored Procedures :
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations.Schema;
using Sample.API.Database;
using Sample.API.Models;
using Sample.API;
using Sample.API.Controllers;
using Sample.Common;
using System.Web.Http;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.Entity.Core.EntityClient;
using System.Collections;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Objects;
namespace Sample.Controllers
{
//[RoutePrefix("users/ProfileSearch/{keyword}")]
public class ProfileSearchController : SampleApiController
{
#region DTO & Validation
public class ProfileSearchRequest
{
public string keyword { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string summary { get; set; }
public int rowfrom { get; set; }
public int rowto { get; set; }
public int count { get; set; }
}
public class ProfileSearchResponseJSON
{
public int rowfrom { get; set; }
public int rowto { get; set; }
public int count { get; set; }
public int pageSize { get; set; }
public int pageing { get; set; }
public List<ProfileSearchResponse> Profilesearch { get; set; }
}
public class ProfileSearchResponse
{
public long id { get; set; }
public long userId { get; set; }
public string keyword { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string summary { get; set; }
public string emailAddress { get; set; }
public string pictureURL { get; set; }
public DateTime? created { get; set; }
public string profileName { get; set; }
public string profileLI { get; set; }
public string city { get; set; }
}
#endregion
//Route
[Route("search/ProfileSearch")]
public dynamic Post(ProfileSearchRequest request)
{
var lstRecSearch = new List<ProfileSearchResponse>();
List<SqlParameter> objSqlParams = new List<SqlParameter>();
string strFiled = string.Empty;
//optional parameters Set Wep Api
if (request.keyword != null)
{
strFiled = " @keyword,";
var keywordParm = new SqlParameter { ParameterName = "keyword", Value = request.keyword };
objSqlParams.Add(keywordParm);
}
if (request.firstname != null && request.firstname!="")
{
strFiled+="@firstname,";
var firstnameParm = new SqlParameter { ParameterName = "firstname", Value = request.firstname };
objSqlParams.Add(firstnameParm);
}
if (request.lastname != null)
{
strFiled += "@lastname,";
var firstnameParm = new SqlParameter { ParameterName = "lastname", Value = request.lastname };
objSqlParams.Add(firstnameParm);
}
if (request.summary != null)
{
strFiled += "@summary,";
var firstnameParm = new SqlParameter { ParameterName = "summary", Value = request.summary };
objSqlParams.Add(firstnameParm);
}
if (request.city !=null)
{
strFiled += "@city,";
var firstnameParm = new SqlParameter { ParameterName = "city", Value = request.city };
objSqlParams.Add(firstnameParm);
}
//Stored Procedures Call
var getlist = SampleDb.Database.SqlQuery<ProfileSearchResponse>("Exec [searchProfiles] " + strFiled.TrimEnd(','), objSqlParams.ToArray());
var _list = new ProfileSearchResponseJSON();
string Cou = WebConfigurationManager.AppSettings["RecSearchpagesize"];
int PageSize = Int32.Parse(Cou.ToString());
_list.pageSize = PageSize;
_list.pageing = _list.count / PageSize;
_list.Profilesearch = getlist.ToList();
_list.count = _list.Profilesearch.Count();
List<ProfileSearchResponseJSON> lstProfileSearch = new List<ProfileSearchResponseJSON>();
lstProfileSearch.Add(_list);
return lstProfileSearch;
}
}
}
Run Program :
using Fiddler
http://localhost:1234/search/Search
Type:Post
User-Agent: Fiddler
Content-Type: application/json;charset=UTF-8
Host: localhost:5321
Content-Length: 18
//Passed Optional parameter
{"keyword":"test","firstname":"aaa"}
(or)
{"keyword":"test"}
No comments:
Post a Comment