CreatedAtRoute routing to different controller

前端 未结 3 1925
闹比i
闹比i 2020-12-13 12:33

I\'m creating a new webapi using attribute routing to create a nested route as so:

    // PUT: api/Channels/5/Messages
    [ResponseType(typeof(void))]
    [         


        
3条回答
  •  时光说笑
    2020-12-13 13:36

    Just adding to the answers above: on Attribute Routing:

    I was caught out by the parameter name, took me an hour to realise that the parameter needs to named correctly otherwise the Url Helper will return null.

    i.e if you have an action method like:

    [Route("api/messages/{id}", Name="GetAction")]
    public IHttpActionResult GetEntity(int mySpecialUniqueId)
    {
        // do some work.
    }
    

    Then the return should be:

    return CreatedAtRoute("GetAction", new { mySpecialUniqueId = entity.Id }, entity);
    

    On the more simple examples, the Id property kept throwing me off so i thought I would expand on it more in this answer to help save others time on this little issue.

    See this more complicated example for more detail:

    Attribute Routing and CreatedAtRoute

提交回复
热议问题