Reputation: 1213
As much as I'm really enjoying the new direction that mvc core is going, one thing I feel like has gone a step back is setting up routes. An application I'm building has upwards of 50 different routes, and putting all of those routes in startup.cs seems like it will become a bit unwieldy at some point due to all of the routes.
Is it at all possible to split the routes into their own object, or middleware as was with previous versions of mvc?
Upvotes: 0
Views: 148
Reputation: 7374
Routes can be defined on each controller directly, using annotations:
namespace Example.WebApp
{
[Route("api/sheep")]
public class SheepController : Controller
{
[HttpGet("{id}")]
public IActionResult Get(long id)
{
return new ObjectResult("Example"+id);
}
[HttpPost]
public void Post([FromBody]ComplexObject obj)
{
}
[HttpGet("horse/{id}/{chicken}")]
public void AnotherGet(long id, string chicken)
{
}
}
}
Using these annotations, you can build up arbitrarily complex URI schemes without all that complexity being in the Startup.cs - however, the downside is that you need to take responsibility for ensuring you don't create conflicting URIs across multiple controllers.
Upvotes: 2