Correct url for controller

211 views Asked by At

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();
        }


}

}

2

There are 2 answers

1
hmiedema9 On

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

0
Sajad Afaghiy On

"Routing to controller actions in ASP.NET Core" and "Routing in ASP.NET Core" are good articles for this purpose.

In your case the correct URL is "http://localhost:5000/api/profile/1".

Note: If you still have problem with Postman, search for how to configure it.