Blazor/API 路由问题 - 入门
Blazor/API Routing Issue - Getting Started
我的 API 一直在这个部分爆炸:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
我觉得跟加个变量(id)有关
[Route("[api/controller]")]
[ApiController]
public class NotificationController : Controller
{
private readonly INotificationService _notificationService;
public NotificationController()
{
_notificationService = Services.NotificationService();
}
[HttpGet("{id}")]
public IActionResult GetNotificationsByAccountId(string id)
{
var notis = _notificationService.FindAllActiveByAccountId(id);
return Ok();
}
}
错误一:
行动:'Solution.Api.Controllers.NotificationController.GetNotificationsByAccountId (Solution.Api)'
错误:处理模板“[api/controller]/{id}”时,找不到标记 'api/controller' 的替换值。可用代币:'action, controller'。要在路由或约束中使用“[”或“]”作为文字字符串,请改用“[[”或“]]”。
想法?
我认为你应该像这样使用 Route 属性:
[Route("api/[controller]")]
路由模板中的[controller]是一个token,ASP.NETCore在添加路由到路由table
时会自动替换
我的 API 一直在这个部分爆炸:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
我觉得跟加个变量(id)有关
[Route("[api/controller]")]
[ApiController]
public class NotificationController : Controller
{
private readonly INotificationService _notificationService;
public NotificationController()
{
_notificationService = Services.NotificationService();
}
[HttpGet("{id}")]
public IActionResult GetNotificationsByAccountId(string id)
{
var notis = _notificationService.FindAllActiveByAccountId(id);
return Ok();
}
}
错误一: 行动:'Solution.Api.Controllers.NotificationController.GetNotificationsByAccountId (Solution.Api)' 错误:处理模板“[api/controller]/{id}”时,找不到标记 'api/controller' 的替换值。可用代币:'action, controller'。要在路由或约束中使用“[”或“]”作为文字字符串,请改用“[[”或“]]”。
想法?
我认为你应该像这样使用 Route 属性:
[Route("api/[controller]")]
路由模板中的[controller]是一个token,ASP.NETCore在添加路由到路由table
时会自动替换