Skip to main content
 首页 » 编程设计

c#之Webapi 2 属性路由不起作用

2024年02月13日32thcjp

我在帐户 Controller 中有一个方法(使用 RoutePrefix("api/Account") 我想响应 api/users 而不是 api/account,所以我使用 '~' 覆盖它:

[HttpGet] 
[AllowAnonymous] 
[Route("~api/users/{name:alpha}/exists")] 
public async Task<IHttpActionResult> UserExists(string name) 
{ 
    var res = await UserManager.FindByNameAsync(name); 
    if (res != null)  
       return Ok(); 
       return BadRequest(); 
} 
 
 
So when I try it out I get the response:  
No HTTP resource was found that matches the request URI 'http://localhost:21975/api/users/johndoe/exists' 

无论如何这应该有效,但不是出于某种原因,任何人都可以提出原因吗?

请您参考如下方法:

路线需要...

[Route("~/api/users/{name:alpha}/exists")]