Wednesday, February 6, 2019

Create Request and Getting Response in webAPI(.Net Framework)


CREATE TABLE [dbo].[olships]( [olid] [int] NOT NULL, [olname] [varchar](50) NULL, [ollogo] [varchar](max) NULL, [Location] [varchar](50) NULL, [shipid] [int] NULL, [shipname] [varchar](50) NULL, [shiplogo] [varchar](max) NULL, PRIMARY KEY CLUSTERED  ( [olid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
====================================================
CREATE TABLE [dbo].[session](
[eventTitle] [varchar](50) NULL,
[eventStatus] [varchar](50) NULL,
[startTime] [varchar](50) NULL,
[endTime] [varchar](50) NULL,
[remainderAdded] [bit] NULL,
[duration] [varchar](50) NULL,
[location] [varchar](50) NULL,
[trainerName] [varchar](50) NULL,
[description] [varchar](max) NULL,
[sessionID] [varchar](50) NOT NULL,
[sessionDate] [datetime] NULL,
[trainerID] [varchar](50) NULL,
PRIMARY KEY CLUSTERED 
(
[sessionID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Model:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TrainingAppWebAPI_Framework_.Models
{
    public class Main
    {
        public int id { get; set; }
        public string operatingLineName { get; set; }
        public string operatingLineLogo { get; set; }
        public string location { get; set; }
        public List<Ship> ships { get; set; }
    }

    public class Ship
    {
        public int id { get; set; }
        public string operatingLineId { get; set; }
        public string shipName { get; set; }
        public string shipLogo { get; set; }
    }
}
======================================================
Controller
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TrainingAppWebAPI_Framework_.Models;

namespace TrainingAppWebAPI_Framework_.Controllers
{
    public class GetshipURLController : ApiController
    {
        [HttpPost]
        public HttpResponseMessage CreateOlShip([FromBody] Main root)
        {
            var Parent = new Dictionary<String, object>();
            var checkin = new Dictionary<String, object>();
            try
            {
                using (TryappEntities1 db = new TryappEntities1())
                {
                    var ols = new olship();
                    ols.olid = root.id;
                    ols.olname = root.operatingLineName;
                    ols.ollogo = root.operatingLineLogo;
                    ols.Location = root.location;
                    ols.shipid = root.ships[0].id;
                    ols.shipname = root.ships[0].shipName;
                    ols.shiplogo = root.ships[0].shipLogo;

                    db.olships.Add(ols);
                    db.SaveChanges();
                }
                Parent.Add("status", "true");

                checkin.Add("id", root.id);
                checkin.Add("baseurl", "http://10.2.1.158:8080/test_training");
                checkin.Add("loginUrl", "/login");
                checkin.Add("viewSessionUrl", "/view_session");
                checkin.Add("createSessionUrl", "/create_session");

                Parent.Add("shipUrl", checkin);

                Parent.Add("operatingLineName", root.operatingLineName);
                Parent.Add("operatingLineId", root.id);
                Parent.Add("shipName", root.ships[0].shipName);
                Parent.Add("shipId", root.ships[0].id);

            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
            return Request.CreateResponse(HttpStatusCode.OK, Parent);
        }
    }
}
The Request and Response will be like this
Here We will post the data from Request and we will get the response the client need
weather the data was saved or not and the status code of the API
Database will come by using Entityframework
Hope You like
Happy coding...

JWT Token in WebAPI

using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Net; using Syst...