New Project

  • DB

    Step 1 : Create DB

    1. Go to Microsoft SQL Studio Management Tool

    2. Click on 'New Query'

    
                        create database EmployeeDB
                        

    Step 2 : Create table

    Expand Databases and DemoDB, right-click the Tables folder, select New > Tableā€¦ as shown below.

    Add fields

    Ctrl + S to save the table

    Step 3 : Set primary key

    select the table and right-click the column and select Set Primary Key from the contextual menu as shown below

  • Project Setup using visual studio

    Step 1 : Create New Project using visual studio 2019

    Select API option and uncheck HTTPs option

    Step 2 : Folder structure

    
                          Properties
                            -launchSetting.json 
                          Controllers
                          appSettings.json 
                          Program.cs 
                          Startup.cs 
    
                        
    1. launchSetting.json : for launching, environment settings and variables
    2. appSettings.json : for database settings
    3. Program.cs : entry point of the project
    4. Setup.cs : include all services

    Step 3: Enable CORS

    Go to Startup.cs file and add the below code in Configure method, which will inject CORS into a container.

    
                        app.UseCors(options => options.AllowAnyOrigin());  
                        

    Add the below code in ConfigureServices method,

    
                        services.AddCors(c =>  
                        {  
                            c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());  
                        });
    
                        

    Step 4: Enable origin

    Go to Startup.cs file and add the below code in Configure method,

    
                        app.UseCors(options=>options.WithOrigins("https://localhost:44342")); 
                        

    Add the below code in ConfigureServices method

    
                        services.AddCors(c =>  
                        {  
                            c.AddPolicy("AllowOrigin", options => options.WithOrigins("https://localhost:44342"));  
                        });
                        
  • Coding

    Step 1 : Create Model

    1. create new folder and named 'Models'

    2. create new class Models/Department.cs

    
    
                        using System;
                        using System.Collections.Generic;
                        using System.Linq;
                        using System.Threading.Tasks;
    
                        namespace WebApplication5.Models
                        {
                            public class Department
                            {
                                public int DepartmentId { get; set; }
    
                                public string DepartmentName { get; set; }
                            }
                        }
    
                        

    Step2: create new controller

    Controllers/DepartmentController.cs

    
                        using Microsoft.AspNetCore.Http;
                        using Microsoft.AspNetCore.Mvc;
                        using System;
                        using System.Collections.Generic;
                        using System.Linq;
                        using System.Threading.Tasks;
                        using WebApplication5.Models;
                        
    
                        namespace WebApplication5.Controllers
                        {
                            [Route("[controller]")]
                            [ApiController]
                            public class DepartmentController : ControllerBase
                            {
                                // GET: /departments/ 
                                [HttpGet]
                                public string Index()
                                {
                                    
                                  
                                    return "This is my <b>default</b> action...";                               
    
                                }
    
                              
                            }
                        }
    
                        

    Call api

    
                        http://localhost:36002/departments