user6321478
user6321478

Reputation:

Web Api get the Name in the Route

[HttpGet]
[Route("api/CheckMainVerified/{mainid}", Name = "CheckMainVerified")]
public IHttpActionResult CheckIfVerified(int mainid)

So I found that there is a Name property on the attribute

but I don't know how to access it.

I want to be able to check to see it someone has access to stored api's in a database table with tokens etc.. So I don't care if it is any of the following

    api/CheckMainVerified/
    CheckMainVerified

What I have no way of knowing it with the {whatever}

Because this api/CheckMainVerified/{mainid} really ends up being in incoming URL of example

          .../api/CheckMainVerified/32342343   

Since there could be so many variations of URLS, it would be a nightmare to check all the params with values

          /api/books/45/outlets/3/sections/abc/location/9

So ideally I would like to be able to just look at "Name" and then place that "CheckMainVerified" in the database table that i look up.

Upvotes: 0

Views: 5294

Answers (3)

Lodlaiden
Lodlaiden

Reputation: 391

Building on the work of @awquadro, here is an Extension method:

public static string GetRouteName (this HttpActionContext ctx)
        {
            var route = ctx.RequestContext.RouteData.Route;

            if (route.DataTokens.TryGetValue("actions", out object value) && value != null)
            {
                var actions = (HttpActionDescriptor[])value;
                var executedAction = actions.FirstOrDefault();
                var routeAttributes = executedAction?.GetCustomAttributes<RouteAttribute>(true) ?? null;

                return routeAttributes.Where(x=> ctx.ControllerContext.RouteData.Route.RouteTemplate.EndsWith(x.Template)).FirstOrDefault().Name;
            }

            return string.Empty;
        }

This can be called as:

 String routeName = ActionContext.GetRouteName();

Upvotes: 0

Ron
Ron

Reputation: 907

Sorry, probably too late for both of you, but the name is found in the HttpActionContext ActionDescriptor ActionName property. You can create a new filter that inherits from ActionFilterAttribute and get the name. The controller name is the base controller (everything up to Controller, e.g., Customer if the controller name is CustomerController) and the methodName is the actual route (e.g., GetCustomer). There is a decent amount of other interesting information in these fields.

using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace WebApplication1.Filters
{
    public class ServiceCallAuthorization : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var serviceName = actionContext.ControllerContext.ControllerDescriptor.ControllerName;
            var methodName = actionContext.ActionDescriptor.ActionName;
        }
    }
}

Upvotes: 0

awquadros
awquadros

Reputation: 529

I tried to get the route name for a custom log inside an ActionFilter and hours later I had succes.

I have overrided the OnActionExecuted from ActionAttribute class that expose a HttpActionExecutedContext object and then have access to the custom attributes from the executed action.

I ended with something like this

private string TryGetRouteName(HttpActionExecutedContext ctx)
{
  var route = ctx.ActionContext.RequestContext.RouteData.Route;

  if (route.DataTokens.TryGetValue("actions", out object value) && value != null)
  {
    var actions = (HttpActionDescriptor[])value;
    var executedAction = actions.FirstOrDefault();
    var routeAttributes = executedAction?.GetCustomAttributes<RouteAttribute>(true) ?? new Collection<RouteAttribute>();

    return routeAttributes.FirstOrDefault()?.Name ?? string.Empty;
  }

  return string.Empty;
}

Upvotes: 1

Related Questions