Programmatically identifying controller/action from a path, using the Routing engine

13 views Asked by At

Is it possible, programmatically, given a path string, to use the .NET routing engine and identify which Controller/Action it belongs to?

This functionality already happens automatically when a request is intercepted by the .NET application.

For example:

namespace MyApplication.Controllers
{
    [Route("users")]
    public class UsersController : ControllerBase
    {
        [HttpGet("{userId}"]
        public IActionResult GetById(int userId)
        {
            // Get user
        }
    }
}

var routeData = GetRouteData("/users/100");

var controller = routeData.Controller; // "UsersController"
var action routeData.Action; // "GetById"
var method = routeData.Method; // "GET"
0

There are 0 answers