Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.
Over the last several years, I haven’t done much work with ASP.NET 4 MVC. I primarily have used ASP.NET Web API and then eventually went full on with Nancy. My main reason for switching to Nancy for creating Web API’s was I wasn’t overly fond of the ASP.NET MVC/WebAPI Routing. I really liked how you you defined routes in Nancy by specifying the route in the constructor of your Module/Controller which was close to the Action. I tweeted this yesterday, and it seems I’m not alone.@codeopinion I’m very fond of attribute-based routing, moving the route definitions close to the action https://t.co/i5G7vOOeu5
— Dave Glick (@daveaglick) September 27, 2016
Attributes
I can’t say I’m a fan of attributes, that’s another blog post entirely, so I wont get into here. But based on my current understanding of ASP.NET Core MVC, you can use Route attributes closer to the action. So for my own experimenting, let’s give this a try and see how well it can work. If you have any questions, please follow me on Twitter.Demo
For reference, I’m using the ASP.NET Core Web API Template that comes with Visual Studio 2015. All the source code for this blog post is on GitHub. If we look at the ValuesController, it defines the base route as being “api/[controller]”. If we translate this, we can make an HTTP call to http://localhost:5000/api/values which should call the Get() method returning an string array.![result1](https://codeopinion.com/wp-content/uploads/2016/09/result1.png)
Custom Action
Now let’s say I want to create a new method and define it a specific route “api/values/getmore” which should call the GetMoreStuff() method. My first beginner thought was to specify a Route attribute on the action/method.![result2](https://codeopinion.com/wp-content/uploads/2016/09/result2.png)
![result3](https://codeopinion.com/wp-content/uploads/2016/09/result3.png)
![result4](https://codeopinion.com/wp-content/uploads/2016/09/result4.png)
![result5](https://codeopinion.com/wp-content/uploads/2016/09/result5.png)
in your more stuff you should have used ~/api/values/getmore. The ~/ forces resolution to the root of the app and registers route override. 😉
Nice. Thanks for the feedback. I’ll check that out and update the post.
@dcomartin:disqus What’s your answer to @drishyakoodathil:disqus question? I also have the same route that I wanted to use but it doesn’t work. How do I setup routes for http post of “api/roles/{id}/addmember”?
You can have the same route path per http method. eg, [HttpPost(“api/roles/{id}/addmember”)]
What is the route for api/users/{id}/friends ?
Can you elaborate?