Multiple Routes

Sometimes, we might get routing requirements like different routes applied to the same controller or action methods. At first, it seems to be very surprising, but in large projects, we might need this kind of routing.

Multiple Routes can be achieved by putting multiple route attributes on the controller as shown in this code snippet:

    [Route("Stocks")] 
    [Route("[controller]")] 
    public class PacktsController : Controller 
    { 
      [HttpGet("Check")]      
      [HttpGet("Available")] 
      public string GetStock() 
      { 
         return "Its multiple route"; 
      } 
    } 

Run the application to see the multiple routes in action. You would verify the multiple routes are working by accessing the endpoints on the browser as /api/stocks/check or /api/packts/available.

Get Mastering ASP.NET Web API now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.