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?