1

How can I get route from controller type?

for example get route of MyTestController & LoosedController from kind of service or something else.

XService.GetRoute<MyTestController>(); // output: /api/x/MyTest
XService.GetRoute<LoosedController>(); // output: /api/loosed-items

[Route("api/x/[controller]")]
public abstract class MyControllerBase : ControllerBase {
    [HttpGet("{id}")]
    public ActionResult Get(int id) {
        return Ok(id);
    }
}

[Route("api/loosed-items")]
public class LoosedController : MyControllerBase {
}

public class MyTestController : MyControllerBase {
    [HttpGet("action")]
    public ActionResult FindThis() {
        return Ok();
    }
}
4
  • check this - stackoverflow.com/questions/363211/… Commented Aug 18, 2020 at 10:07
  • @SSD i just have controller type, i must take route just with Type. Commented Aug 18, 2020 at 10:11
  • You can use reflection logic and retrieve what you want. check this - stackoverflow.com/questions/21583278/… Commented Aug 18, 2020 at 10:21
  • @SSD its not useful, reflaction cant handle routeTemplate and RouteAttribute inheritance Commented Aug 18, 2020 at 10:38

1 Answer 1

1

According to your description, you could try to use IActionDescriptorCollectionProvider to get all controller routes.

More details, you could refer to below codes:

Interface:

public interface IGetRoute
{
    IList<RouteModel> GetRotue(string type);
}

Class: public class GetRoute : IGetRoute { private readonly IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider;

    public GetRoute(IActionDescriptorCollectionProvider actionDescriptorCollectionProvider)
    {
        _actionDescriptorCollectionProvider = actionDescriptorCollectionProvider;
    }


    public IList<RouteModel> GetRotue(string type)
    {

        var typename = type.Replace("Controller", "");

        List<RouteModel> result = new List<RouteModel>();

        foreach (ActionDescriptor _e in _actionDescriptorCollectionProvider.ActionDescriptors.Items)
        {

            if (_e.AttributeRouteInfo != null)
            {
                if (_e.AttributeRouteInfo.Template.Contains(typename))
                {
                    result.Add(new RouteModel() { Name = _e.AttributeRouteInfo.Template, ControllerName = typename });
                }
            } else if (_e is ControllerActionDescriptor)
            {
                var e = _e as ControllerActionDescriptor;
                if (e.ControllerName == typename)
                {
                    result.Add(new RouteModel() { Name = $"/{e.ControllerName}/{e.ActionName}", ControllerName = typename });
                }

            }


        }

        return result;
    }
}

Register in ConfigureServices method:

        services.AddScoped<IGetRoute, GetRoute>();

Usage:

        var res = _route.GetRotue(typeof(MyTestController).Name);

        var res2 = _route.GetRotue(typeof(HomeController).Name);

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

This answer fails to take into account the route template and associated values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.