could you please tell what is right url for the controller method GetProfileById with attribute 'id' I tried many urls like 'http://localhost:5000/api/Profile/GetProfileById/1', 'http://localhost:5000/api/Profile/ProfileById/1','http://localhost:5000/api/ProfileById/1' but Postman shows message 'Cannot Get and my url'
Here is code:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SocialNetwork.Repositories;
using SocialNetwork.Repositories.GenericRepository;
namespace SocialNetwork.Controllers
{
[Route("/api/[controller]")]
public class ProfileController : Controller
{
private readonly ProfileRepository repository;
public ProfileController(IProfileRepository repository)
{
this.repository = (ProfileRepository)repository;
}
[HttpGet("{id}")]
[ProducesResponseType(200, Type = typeof(Profile))]
[ProducesResponseType(404)]
public async Task<ActionResult<Profile>> GetProfileById(int id)
{
var profile = await repository.GetById(id);
if(profile != null)
{
return new OkObjectResult(Json(profile));
}
return NotFound();
}
}
}
It should be
/api/profile/<id>. You can tell this by the[Route("/api/[controller]")]directive above the method. Then, any id's are added on after one more following slash.So your full route with
localhost(or your domain) would be http://localhost:5000/api/Profile/1