【问题标题】:Blazor/API Routing Issue - Getting StartedBlazor/API 路由问题 - 入门
【发布时间】:2020-11-22 18:28:28
【问题描述】:

我的 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();
            }

}

错误 1: 行动:'Solution.Api.Controllers.NotificationController.GetNotificationsByAccountId (Solution.Api)' 错误:处理模板“[api/controller]/{id}”时,找不到令牌“api/controller”的替换值。可用令牌:“动作,控制器”。要在路由或约束中使用 '[' 或 ']' 作为文字字符串,请改用 '[[' 或 ']]'。

想法?

【问题讨论】:

  • 您好@D-W,如果我的回复对您有帮助,您可以接受,谢谢:)。

标签: asp.net-core asp.net-web-api model-view-controller


【解决方案1】:

我认为你应该像这样使用 Route 属性:

[Route("api/[controller]")]

路由模板中的[controller]是一个token,ASP.NET Core在将路由添加到路由表时会自动替换

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 2013-11-06
    • 2014-11-27
    • 2013-08-30
    • 1970-01-01
    相关资源
    最近更新 更多