1. Add Model Class
- namespace testmvcapp.Models {
- public class ClassModel {
- public string StdName {
- get;
- set;
- }
- }
- }

2. Modify on WebApiConfig
- public static class WebApiConfig {
- public static void Register(HttpConfiguration config) {
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{sLookUpString}",
- defaults: new {
- sLookUpString = RouteParameter.Optional
- });
- }
- }
![]()
3. Add a controller and set the method to return string
- public class BooksController: ApiController {
- // LIST OBJECT WILL HOLD AND RETURN A LIST OF BOOKS.
- List < ClassModel > MyBooks = new List < ClassModel > ();
- // RETURN A LIST OF BOOKS MATCHING WITH THE REQUESTED ALPHANUMERIC VALUE(S).
- public IEnumerable < ClassModel > Get(string sLookUpString) {
- GetTheBooks(sLookUpString);
- return MyBooks;
- }
- public void GetTheBooks(string sFind) {
- string sConnString = "";
- SqlConnection myConn = new SqlConnection(sConnString);
- // SEARCH DATABASE TABLE MATCHING BOOKS WITH THE "LOOKUP" STRING.
- SqlCommand objComm = new SqlCommand("SELECT * FROM dbo.studentmaster where StdName like '" + sFind + "%' order by StdName desc ", myConn);
- myConn.Open();
- // objComm.Parameters.AddWithValue("@LookUP", sFind);
- SqlDataReader reader = objComm.ExecuteReader();
- // ADD EACH BOOKNAME ALONG WITH ITS ID IN THE LIST.
- while (reader.Read()) {
- MyBooks.Add(new ClassModel {
- StdName = reader["StdName"].ToString()
- });
- }
- myConn.Close();
- }
- }




Join the conversation! Your thoughts help the community grow.