【发布时间】:2020-05-06 21:18:28
【问题描述】:
我有一个名为“GebruikersController”的控制器类
当我使用“https://localhost:5001/Gebruikers”时,我得到了正确的输出,但是当我使用“https://localhost:5001/api/GebruikerController/Gebruikers”时(它应该如何工作?)我得到一个空白页。有谁能够帮我?谢谢!
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;
}
}
}
【问题讨论】:
标签: c# asp.net-core controller