hacklink al hack forum organik hit kayseri escort b-ok zeta library books z librarygrandpashabetgrandpashabetjojobet güncel girişjojobet 1019bahiscasinobetturkeySlot Oyunlarıankara escortlidoSoft2betdeneme bonusu veren sitelerradissonbetjojobetdinamobet girişjojobetjojobetjojobetjojobet girişjojobet girişjojobet girişpadişahbet girişjojobet,jojobet giriş,jojobet güncel giriş,jojobet resmi girişjojobetparibahisbaşarıbetcasibom

Post Details Page

At Devhq.in, we provide comprehensive digital solutions for businesses. From software development and app creation to social media management and brand design, we help you build and grow your digital presence.

Student Api With Crud Operation with Dot Net Code StudentController

Make a StudentController.cs file and add this code

  private readonly IStudent _studentService;

  public StudentController(IStudent studentService)
  {
      _studentService = studentService;
  }
C#

This code give access to use all function inside the Istudent interface.

Now Lets Write Crud Operation code for Student

⭐Read Student (Get Student)


        [HttpGet]
        public async Task<ActionResult<List<Student>>> Get()
        {
            return await _studentService.GetStudents();
        }
C#

⭐Search Student (Get one Student )

    [HttpGet("{id:length(24)}", Name = "GetStudent")]
    public async Task<ActionResult<Student>> Get(string id)
    {
        var student = await _studentService.GetStudent(id);

        if (student == null)
        {
            return NotFound();
        }

        return student;
    }
C#

⭐Add Student (Post Student)

 [HttpPost]
 public async Task<ActionResult<Student>> Create(Student student)
 {
     await _studentService.CreateStudent(student);

     return CreatedAtRoute("GetStudent", new { id = student.Id.ToString() }, student);
 }
C#

⭐Update Student (Put Student)

 [HttpPut("{id:length(24)}")]
 public async Task<IActionResult> Update(string id, Student studentIn)
 {
     var student = _studentService.GetStudent(id);

     if (student == null)
     {
         return NotFound();
     }

     await _studentService.UpdateStudent(id, studentIn);

     return NoContent();
 }
C#

⭐Delete Student (delete Student)


        [HttpDelete("{id:length(24)}")]
        public async Task<IActionResult> Delete(string id)
        {
            var student = _studentService.GetStudent(id);

            if (student == null)
            {
                return NotFound();
            }

            await _studentService.DeleteStudent(id);

            return NoContent();
        }       
C#

Full Code will look like this

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Univercity.Models;
using Univercity.Services.Interfaces;

namespace Univercity.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        private readonly IStudent _studentService;

        public StudentController(IStudent studentService)
        {
            _studentService = studentService;
        }

        [HttpGet]
        public async Task<ActionResult<List<Student>>> Get()
        {
            return await _studentService.GetStudents();
        }

        [HttpGet("{id:length(24)}", Name = "GetStudent")]
        public async Task<ActionResult<Student>> Get(string id)
        {
            var student = await _studentService.GetStudent(id);

            if (student == null)
            {
                return NotFound();
            }

            return student;
        }

        [HttpPost]
        public async Task<ActionResult<Student>> Create(Student student)
        {
            await _studentService.CreateStudent(student);

            return CreatedAtRoute("GetStudent", new { id = student.Id.ToString() }, student);
        }

        [HttpPut("{id:length(24)}")]
        public async Task<IActionResult> Update(string id, Student studentIn)
        {
            var student = _studentService.GetStudent(id);

            if (student == null)
            {
                return NotFound();
            }

            await _studentService.UpdateStudent(id, studentIn);

            return NoContent();
        }

        [HttpDelete("{id:length(24)}")]
        public async Task<IActionResult> Delete(string id)
        {
            var student = _studentService.GetStudent(id);

            if (student == null)
            {
                return NotFound();
            }

            await _studentService.DeleteStudent(id);

            return NoContent();
        }       
    }
}
C#

Share now :

Facebook
Twitter
LinkedIn
Review Your Cart
0
Add Coupon Code
Subtotal