Step-1:
I already discuss the table structure in my previous article. Please follow that article for creating a table structure using the Code-first entity framework. Also, I discuss the migration of models and updating your database in the SQL server.
Step-2:
In your ASP.NET Core application, Create a Controller Page "ProfileController" inside the Controller folder. Then write API for retrieving a single record from the "Profile" table.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProfileWebApi.Model;
using Microsoft.EntityFrameworkCore;
namespace ProfileWebApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ProfileController : ControllerBase
{
public ApplicationDbContext DbContext;
public ProfileController(ApplicationDbContext applicationDbContext)
{
DbContext = applicationDbContext;
}
[HttpGet]
public ActionResult<Profile> GetProfileById(long id)
{
var res = DbContext.profiles.Where(a=>a.ID==id).FirstOrDefault();
return res;
}
}
}
Step-3:
Then Build your project. After build successfully completed then click on start debugging. then edit your URL. Run your localhost URL with "/swagger/index.html ". For example:
Step-4:
Then this page will open.
Step-5:
Click on "GET:/api//api/Profile/GetProfileById". Then this page will open:
Click on the "Try It Out" button. Another button "Execute" will show then click on that button.
Step-6:
The result of your API will show. i.e. the retrieve data will show.
0 Comments