C# 为什么ApicController中的路由不工作?
我有一个名为“GebruikerController”的控制器类 当我使用“”时,我得到正确的输出,但当我使用“”(它应该如何工作?)时,我得到一个空白页。有人能帮我吗?谢谢C# 为什么ApicController中的路由不工作?,c#,asp.net-core,controller,C#,Asp.net Core,Controller,我有一个名为“GebruikerController”的控制器类 当我使用“”时,我得到正确的输出,但当我使用“”(它应该如何工作?)时,我得到一个空白页。有人能帮我吗?谢谢 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using RESTAPI.Data; using RESTAPI
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用原始数据;
使用RESTAPI.Data.Repositories;
使用RESTAPI.DTOs;
使用PI模型;
命名空间RESTAPI.Controllers
{
[ApiConventionType(typeof(DefaultApiConventions))]
[产生(“应用程序/json”)]
[路由(“api/[控制器]”)]
[ApiController]
公共类GebruikersController:ControllerBase
{
私人只读IGebruikerRepository(U gebruikerRepository);
公共GebruikerController(IGebuikerRepository上下文)
{
_gebruikerRepository=上下文;
}
//获取:api/Gebruikers
///
///吉夫特·阿赫特纳姆乔治登酒店
///
///范盖布雷克斯阵列
[HttpGet(“/Gebruikers”)]
公共IEnumerable GetGebruikers()
{
return _gebruikerRepository.GetAlleGebruikers().OrderBy(d=>d.Achternaam);
}
//获取:api/Gebruikers/5
///
///Geeft de gebruiker遇到了他
///
///赫特·范德格布吕克
///德格布吕克
[HttpGet(“{id}”)]
公共行动结果GetGebruiker(内部id)
{
Gebruiker-Gebruiker=\u gebruikerRepository.GetBy(id);
if(gebruiker==null)返回NotFound();
返回gebruiker;
}
}
}
尝试使用
https://localhost:5001/api/Gebruikers/Gebruikers
而不是
https://localhost:5001/api/GebruikerController/Gebruikers
URL中没有写“控制器”一词。尝试使用
https://localhost:5001/api/Gebruikers/Gebruikers
而不是
https://localhost:5001/api/GebruikerController/Gebruikers
URL中未写入“控制器”一词。在操作上添加路由属性。然后,两条路线都将被识别
[HttpGet("/Gebruikers")]
[Route("Gebruikers")]
public IActionResult GetGebruikers()
{
return Ok();
}
斜杠将忽略控制器上的路由属性,因此它只能识别路由:“在操作上添加路由属性”。然后,两条路线都将被识别
[HttpGet("/Gebruikers")]
[Route("Gebruikers")]
public IActionResult GetGebruikers()
{
return Ok();
}
斜杠将忽略控制器上的route属性,因此它只能识别路由:“当我使用该路由时,会出现错误。{“type”:“tools.ietf.org/html/…发生了多个验证错误。”,“status”:400,“traceId”:“| 50c831bb-4dab87ead88f6d56。”,“errors”:{“id”:[“值'Gebruikers'无效。”]}}实际上,控制器的名称是带有s的GebruikerController,您觉得如何?@user2768479我使用了那个,然后我得到了一个错误{“type”:“tools.ietf.org/html/…或多个验证错误发生。”,“status”:400,“traceId”:“| 50c831bb-4dab87ead88f6d56。”,“errors”:{“id”:[“值'Gebruikers'无效。”]}}当我使用该值时,我会收到一个错误。{“type”:“tools.ietf.org/html/…或多个验证错误发生。”,“status”:400,“traceId”:“{值“Gebruikers”无效。“]}}实际上控制器的名称是带有s的Gebruikers控制器,您的情况如何?@user2768479我使用了该值,然后我得到一个错误{“type”:“tools.ietf.org/html/…或多个验证错误发生了。”,“status”:400,“traceId”:“| 50c831bb-4dab87ead88f6d56”,“errors”:{“id:”值“Gebruikers”无效。“]}