如何从ActionExecutingContext获取路由模板

时间:2017-01-19 08:28:21

标签: c# asp.net-web-api asp.net-core asp.net-core-webapi

我需要通过http方法和路由模板限制对控制器方法的访问。如何从 context var?

获取路径模板
public abstract class BaseController : Controller
    {
      public override void OnActionExecuting(ActionExecutingContext context)
      {

        string apiKey = context.HttpContext.Request.Headers["key"];
        string httpMethod = context.HttpContext.Request.Method.ToUpper();
        string routeTemplate = context. ???

        if (CheckAccess(apiKey, httpMethod , routeTemplate))
        {
           context.Result = Forbid();
        }
      }
    }

1 个答案:

答案 0 :(得分:2)

我已经解决了这个问题。

public abstract class BaseController : Controller
{
  public override void OnActionExecuting(ActionExecutingContext context)
  {

    string apiKey = context.HttpContext.Request.Headers["key"];
    string httpMethod = context.HttpContext.Request.Method.ToUpper();
    string routeTemplate =context.ActionDescriptor.AttributeRouteInfo.Template;

    if (CheckAccess(apiKey, httpMethod , routeTemplate))
    {
       context.Result = Forbid();
    }
  }
}