2017-01-19 89 views
2

我需要通過http方法和路由模板限制對控制器方法的訪問。如何從獲取路線模板背景 var?如何從ActionExecutingContext獲取路由模板

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

我已解決此問題。

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(); 
    } 
    } 
}