Reputation: 37
I have a controllerclass called "GebruikersController"
when I use "https://localhost:5001/Gebruikers" I get the right output but when I use "https://localhost:5001/api/GebruikerController/Gebruikers" (how it should work?) I get a blank page. Can anybody help me? Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using RESTAPI.Data;
using RESTAPI.Data.Repositories;
using RESTAPI.DTOs;
using RESTAPI.Models;
namespace RESTAPI.Controllers
{
[ApiConventionType(typeof(DefaultApiConventions))]
[Produces("application/json")]
[Route("api/[controller]")]
[ApiController]
public class GebruikersController : ControllerBase
{
private readonly IGebruikerRepository _gebruikerRepository;
public GebruikersController(IGebruikerRepository context)
{
_gebruikerRepository = context;
}
// GET: api/Gebruikers
/// <summary>
/// Geeft alle gebruikers geordend op achternaam
/// </summary>
/// <returns>array van gebruikers</returns>
[HttpGet("/Gebruikers")]
public IEnumerable<Gebruiker> GetGebruikers()
{
return _gebruikerRepository.GetAlleGebruikers().OrderBy(d => d.Achternaam);
}
// GET: api/Gebruikers/5
/// <summary>
/// Geeft de gebruiker met het gegeven id terug
/// </summary>
/// <param name="id">het id van de gebruiker</param>
/// <returns>De gebruiker</returns>
[HttpGet("{id}")]
public ActionResult<Gebruiker> GetGebruiker(int id)
{
Gebruiker gebruiker = _gebruikerRepository.GetBy(id);
if (gebruiker == null) return NotFound();
return gebruiker;
}
}
}
Upvotes: 1
Views: 179
Reputation: 66
Try to use
https://localhost:5001/api/Gebruikers/Gebruikers
instead of
https://localhost:5001/api/GebruikerController/Gebruikers
.
The "controller" word is not written in the URL.
Upvotes: 5
Reputation: 8459
Add a Route attribute on the action. then, both routes will be recognized.
[HttpGet("/Gebruikers")]
[Route("Gebruikers")]
public IActionResult GetGebruikers()
{
return Ok();
}
The slash will ignore the route attribute on your controller, so it can only recognize the route: "https://localhost:5001/Gebruikers"
Upvotes: 0