1

I have an ASP.Net Core MVC app with a Web API controller, I believe i provided the right routes and everything but i still get a 404 error when calling the any API of the controller in Postman

here is how the controller looks:

[Route("api/[controller]")]
    [ApiController]
    public class PetsController : Controller
    {
        private readonly IPetService _petService;

        public PetsController(IPetService _petService)
        {
            this._petService = _petService;
        }

        [HttpPost("Activation")]
        public async Task<IActionResult> Activation(Guid id)
        {
            var serviceResult = await _petService.DeletePet(id);
            return Ok(serviceResult);
        }

        [HttpGet("GetPet/{id}")]
        public async Task<IActionResult> GetPet(Guid id)
        {
            var serviceResult = await _petService.GetPetDetails(id);
            return Ok(serviceResult);
        }

        [HttpGet("GetPets/{pageSize}/{pageIndex}/{keyWord}")]
        public async Task<IActionResult> GetPets(int pageSize, int pageIndex, string keyWord)
        {
            var serviceResult = await _petService.GetPets(pageSize, pageIndex, keyWord);
            return Ok(serviceResult);
        }

        [HttpPost("SavePet")]
        public async Task<IActionResult> SavePet(PetDTO pet)
        {
            var serviceResult = (Object)null;

            if (!pet.Id.HasValue)
                serviceResult = await _petService.CreatePet(pet);
            else
                serviceResult = await _petService.EditPet(pet);

            return Ok(serviceResult);
        }

        [HttpGet("GetPetDues/{id}")]
        public async Task<IActionResult> GetPetDues(Guid id)
        {
            var serviceResult = await _petService.GetPetDues(id);
            return Ok(serviceResult);
        }

        [HttpGet("GetPetAppointments/{id}")]
        public async Task<IActionResult> GetPetAppointments(Guid id)
        {
            var serviceResult = await _petService.GetPetAppointments(id);
            return Ok(serviceResult);
        }

        [HttpGet("GetPetDiseases/{petId}")]
        public async Task<IActionResult> GetPetDiseases(Guid petId)
        {
            var serviceResult = await _petService.GetPetDiseases(petId);
            return Ok(serviceResult);
        }
    }

Launch settings:

"iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:50521",
      "sslPort": 44310
    }

and this is an examply on how i call an API in Postman: http://localhost:50521/api/pets/GetPets/10/0/

I believe I should get a 200 OK but still im getting a 404 error! How can i fix this??

2
  • How do you start the application? Commented Oct 3, 2019 at 15:36
  • @Nicola I press on IIS Express Commented Oct 3, 2019 at 16:41

1 Answer 1

1

Route template was not used correctly

[HttpGet("GetPets/{pageSize}/{pageIndex}/{keyWord?}")]
public async Task<IActionResult> GetPets(int pageSize, int pageIndex, string keyWord = null)
{
    var serviceResult = await _petService.GetPets(pageSize, pageIndex, keyWord);
    return Ok(serviceResult);
}

The original route used was missing the template parameters.

In fact all of the shown routes are missing their parameters in the route template.

Route template reference

Tokens within curly braces ({ ... }) define route parameters that are bound if the route is matched. You can define more than one route parameter in a route segment, but they must be separated by a literal value. For example, {controller=Home}{action=Index} isn't a valid route, since there's no literal value between {controller} and {action}. These route parameters must have a name and may have additional attributes specified.

Literal text other than route parameters (for example, {id}) and the path separator / must match the text in the URL. Text matching is case-insensitive and based on the decoded representation of the URLs path. To match a literal route parameter delimiter ({ or }), escape the delimiter by repeating the character ({{ or }}).

Reference Routing in ASP.NET Core

Reference Routing to controller actions in ASP.NET Core

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for pointing it out and I fixed it immediately but still i'm getting a 404 error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.