Answered by:
CreatedAtRoute using Attribute-based routing and Unit Testing

Question
-
User-638259267 posted
I found this code example http://www.asp.net/web-api/overview/testing-and-debugging/unit-testing-controllers-in-web-api which addresses using the CreatedAtRoute return type for IHttpActionResult when using default routing as configured in WebApiConfig.cs, however, it does not address how to handle this while using Attribute-based routing.
How does the CreatedAtRoute method need to be populated correctly when using Attribute-based routing and consequently how is this supposed to be Unit Tested in this scenario also?
Please advise.
Thanks.
Tuesday, August 2, 2016 3:55 PM
Answers
-
User36583972 posted
Hi vs2010junkie,
does the CreatedAtRoute method need to be populated correctly when using Attribute-based routingYou can try to give the route a name and pass that in to the CreatedAtRoute method. This is done by setting a Name property on the Route. You can call the CreatedAtRoute method from the PostMessage action and pass in the route name.
The following code for your reference.
[Route("api/values/{id}", Name = "GetMessage")] public IHttpActionResult PostMessage(Message message) { return CreatedAtRoute("GetMessage", new { id = message.ID }, message); //return CreatedAtRoute("DefaultApi", new { id = message.ID }, message); }
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 3, 2016 5:37 AM
All replies
-
User36583972 posted
Hi vs2010junkie,
does the CreatedAtRoute method need to be populated correctly when using Attribute-based routingYou can try to give the route a name and pass that in to the CreatedAtRoute method. This is done by setting a Name property on the Route. You can call the CreatedAtRoute method from the PostMessage action and pass in the route name.
The following code for your reference.
[Route("api/values/{id}", Name = "GetMessage")] public IHttpActionResult PostMessage(Message message) { return CreatedAtRoute("GetMessage", new { id = message.ID }, message); //return CreatedAtRoute("DefaultApi", new { id = message.ID }, message); }
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 3, 2016 5:37 AM -
User-638259267 posted
If I have numerous Web API Controllers all named the same (Get, Post, Put, Delete) and specify a Route Name that all match "Post", "Put", "Delete", "Get" , "GetById", will this cause a conflict when doing the routing or do all of these names have to be unique per method in each Web API Controller?
Please clarify.
Thanks.
Friday, August 5, 2016 3:40 PM -
User36583972 posted
Hi vs2010junkie,
will this cause a conflict when doing the routing or do all of these names have to be unique per method in each Web API Controller?This is not a conflict because each of us is a unique controller. You can refer the following tutorials about Web API Routing.
How ASP.NET Web API routes HTTP requests to controllers and actions:
http://www.asp.net/web-api/overview/web-api-routing-and-actions
Best Regards,
Yohann Lu
Saturday, August 6, 2016 5:17 AM