【问题标题】:Webapi 2 attribute routing not workingWebapi 2属性路由不起作用
【发布时间】:2014-03-25 05:55:22
【问题描述】:

我在 Account 控制器中有一个方法(使用 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'

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

【问题讨论】:

    标签: c# asp.net .net asp.net-web-api asp.net-mvc-5


    【解决方案1】:

    路线需要...

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

    【讨论】:

    • 正要发布这个。您缺少相对根之后的第一个 /
    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 2015-09-09
    • 1970-01-01
    • 2021-06-10
    • 2013-11-11
    相关资源
    最近更新 更多