【问题标题】:Renaming a Web API controller has broken POST routing重命名 Web API 控制器破坏了 POST 路由
【发布时间】:2016-02-29 17:14:13
【问题描述】:

我有一个 Web API 控制器向我决定重命名的 Angular SPA 提供数据,使用 VS 2015 的重命名功能来更新我的所有引用。结果,我无法再向它发布(GET 仍然有效),我不断在客户端收到以下错误:

"A route named 'api/Trainees' could not be found in the route collection.
Parameter name: name"

这个控制器以前使用默认路由可以正常工作 - RouteConfig.cs 中没有任何内容,也没有属性路由,这使它变得陌生。我假设 ASP.NET 仅根据控制器和方法名称计算出默认路由,还是在任何地方缓存路由信息?

我的控制器如下所示:

public class TraineesController : ApiController
{
    private UserRepository repo = new UserRepository();

    // GET: api/Trainees
    public IEnumerable<Trainee> Get()
    {
        return this.repo.GetUsers();
    }

    // GET: api/Trainees/5
    public Trainee Get(string email)
    {
        if (!string.IsNullOrEmpty(email))
        {
            return this.repo.GetUser(email);
        }

        throw new ArgumentNullException("email");
    }

    // POST: api/Trainees
    public IHttpActionResult Post([FromBody]Trainee value)
    {
        try
        {
            this.repo.AddUser(value);
            return CreatedAtRoute("api/Trainees", value.Email, value);
        }
        catch (ArgumentOutOfRangeException)
        {
            return Conflict();
        }
    }

    // PUT: api/Trainees/5
    public IHttpActionResult Put([FromBody]Trainee value)
    {
        try
        {
            this.repo.UpdateUser(value);
            return StatusCode(HttpStatusCode.NoContent);
        }
        catch (ArgumentOutOfRangeException)
        {
            return NotFound();
        }
    }

    // DELETE: api/Trainees/5
    public IHttpActionResult Delete(string email)
    {
        try
        {
            this.repo.RemoveUser(email);
            return StatusCode(HttpStatusCode.NoContent);
        }
        catch (ArgumentOutOfRangeException)
        {
            return NotFound();
        }
    }
}

这是失败的请求(由 Angular $resource 生成):

Request:    POST /api/Trainees HTTP/1.1
Content-Type:   application/json;charset=utf-8
Accept: application/json, text/plain, */*
Referer:    http://localhost:11423/
Accept-Language:    en-GB
Accept-Encoding:    gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Host:   localhost:11423
Content-Length: 64
DNT:    1
Connection: Keep-Alive
Cache-Control:  no-cache

主体:

{"Email":"tony@starkindustries.com","DisplayName":"Tony Stark"}

【问题讨论】:

  • 你能显示完整的 uri 请求吗?
  • 是的,我已经更新了问题。
  • 你能尝试 3 件事吗,因为我看不出有什么问题。第一个是用“api/trainess”小写T调用api第二个添加路由配置config.MapHttpAttributeRoutes();只是为了看到第三个是尝试控制器上的路由前缀[RoutePrefix("api/trainees")] public class TraineesController : ApiController和[HttpPost,Route("")] public IHttpActionResult Post([FromBody]Trainee value)。不要同时尝试每一个
  • 恐怕这些都没有用! MapHttpAttributeRoutes() 已经设置好了,所以我更疑惑为什么显式指定路由属性不起作用。
  • 我已经解决了。它找不到的路线是CreatedAtRoute("api/Trainees", value.Email, value); 中的路线我不知道为什么我找不到它,但我已经用Created 替换了CreatedAtRoute,它似乎工作正常。

标签: asp.net asp.net-web-api routing


【解决方案1】:

有点晚了,但发生这种情况是因为您试图将route template 作为CreatedAtRoute 的第一个参数传递

return CreatedAtRoute("api/Trainees", value.Email, value);

您需要传递一个路由名称。您可以使用 RouteAttribute

命名您的路线
[Route("api/Trainees", Name="TraineesRoute")]`

【讨论】:

    猜你喜欢
    • 2015-05-04
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    相关资源
    最近更新 更多