3

I'm trying to implement 'route grouping' of my controllers by custom attribute, similar like ApiVersioning.

What I would like to have on my controllers:

[Authorize]
[RoutePrefix("api/g{group:apiGroup}/v{version:apiVersion}/Authorization")]
public class AuthorizationController : ApiController
{
    [HttpPost]
    [AllowAnonymous]
    [ApiVersion("1.0")]
    [ApiGroup("NAS")] /// <--- MY GROUPING
    [Route("VerifyCredentials")]
    [ResponseType(typeof(ContactPerson))]
    public async Task<IHttpActionResult> VerifyCredentials(Credentials credentials)
    {....}

So route to this Api call would be localhost/Api/NAS/V1/Authorization

I was succesful with the versioning using the Microsoft.AspNet.WebApi.Versioning package.

My custom Attribute:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ApiGroupAttribute : Attribute
{
    public string GroupName { get; private set; }

    public ApiGroupAttribute(string groupName)
    {
        if (string.IsNullOrEmpty(groupName))
        {
            throw new ArgumentNullException("groupName");
        }
        GroupName = groupName;
    }
}

Edit: Ive tried to achieve this with routeConstraint but learned it does not work like that. How do you achieve this 'Dynamic' routing?

Edit: So after some digging inside the code, I found out I have to imlement my own DirectRouteProvider, any help with that?

1 Answer 1

1

So it seems I just solved it.. Heres my code:

public class CentralizedRouteProvider : DefaultDirectRouteProvider
{
    protected override IReadOnlyList<RouteEntry> GetActionDirectRoutes(HttpActionDescriptor actionDescriptor,IReadOnlyList<IDirectRouteFactory> factories, IInlineConstraintResolver constraintResolver)
    {
        var result = base.GetActionDirectRoutes(actionDescriptor, factories, constraintResolver).ToList();
        var list = new List<RouteEntry>();

        foreach (var route in result.Where(r => r.Route.RouteTemplate.Contains("[ApiGroup]")))
        {
            var attribute = ((ReflectedHttpActionDescriptor)actionDescriptor).GetCustomAttributes<ApiGroupAttribute>().First();

            var newTemplate = route.Route.RouteTemplate.Replace("[ApiGroup]", attribute.GroupName);

            if (!result.Any(r => r.Route.RouteTemplate == newTemplate))
            {
                var entry = new RouteEntry(null, new HttpRoute(newTemplate,
                    new HttpRouteValueDictionary(route.Route.Defaults),
                    new HttpRouteValueDictionary(route.Route.Constraints),
                    new HttpRouteValueDictionary(route.Route.DataTokens)));
                list.Add(entry);
            }
        }

        return list.AsReadOnly();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.