Attribute Routing and CreatedAtRoute

前端 未结 1 832
我寻月下人不归
我寻月下人不归 2020-12-20 12:42

I am trying to convert my Web Api project to use attribute routing. One thing I am not understanding is the CreatedAtRoute method for a POST request. In my WebApiConfig.cs

相关标签:
1条回答
  • 2020-12-20 12:54

    Ok...this was easy once you see whats going on. In attribute routing you have to specify the Name of the route to retrieve the resource. So on my GET action it looks like this:

    [Route("{sessionId}",Name="GetSession")]
        [ResponseType(typeof(Session))]
        public async Task<IHttpActionResult> Get(HttpRequestMessage request, int accountId, int siteId, Guid visitorId, Guid sessionId)
    

    And then in the POST action change the CreatedAtRoute from:

    return CreatedAtRoute("DefaultApi", new
            {
               controller: "session"
                visitorId = session.VisitorId,
                sessionId = session.SessionId
            }, session);
    

    To this:

    return CreatedAtRoute("GetSession", new
            {
                visitorId = session.VisitorId,
                sessionId = session.SessionId
            }, session);
    
    0 讨论(0)
提交回复
热议问题