0

我有一个ASP .Net Core 1.1 MVC Web Api。其中,我有一个似乎没有工作的控制器。当我找到它的行动之一(它只有一个),它不触发:MVC Web API控制器没有触发的操作

namespace InspectionsWebApi.Controllers 
{ 
    [Produces("application/json")] 
    [Route("api/ValidateUsers")] 
    public class ValidateUsersController : Controller 
    { 
     private readonly InspectionsContext _context; 
     private readonly ILogger<UsersController> _logger; 

     public ValidateUsersController(InspectionsContext context, ILogger<UsersController> logger) 
     { 
      _context = context; 
      _logger = logger; 
     } 

     // GET: api/ValidateUsers/abcde12345 
     [HttpGet("nameIdentifier")] 
     public async Task<IActionResult> ValidateUser([FromRoute] string nameIdentifier) 
     { 
      // This code never fires 
     } 
    } 
} 

和我浏览到

http://localhost:50082/api/validateusers/john123

我得到一个404错误。

回答

1

你忘了花括号,使之成为路线值:

[HttpGet("{nameIdentifier}")] 

通过指定它没有花括号,它希望像http://localhost:50082/api/validateusers/nameIdentifier的URL。

+0

谢谢sooo mcuh juunas!这个窍门!我真的认为花括号使参数可选? – user1900799

相关问题