我为Webapi
动作方法编写了一个过滤器。
public void OnActionExecuting(HttpActionContext actionContext)
{
//Log area name here by calling the helper method.
var areaNam = Helpers.GetAreaName(actionContext);
}
我想通过编写一个好的辅助方法来检索区域名称。
public static string GetAreaName(System.Web.Http.Controllers.HttpActionContext httpContext)
{
var areaName = string.Empty;
var routeData = httpContext.ControllerContext.RouteData;
if (routeData != null)
{
areaName = routeData.Route.DataTokens["area"] as string;
}
return areaName;
}
但areaName
始终为空或空。
任何人都可以对此有所了解吗?
答案 0 :(得分:0)
您希望Values
不是DataTokens
。
public static string GetAreaName(System.Web.Http.Controllers.HttpActionContext httpContext)
{
var areaName = string.Empty;
var routeData = httpContext.ControllerContext.RouteData;
if (routeData != null)
{
areaName = routeData.Route.Values["area"] as string;
}
return areaName;
}
Documentation - RouteData.Values
获取路由的URL参数值和默认值的集合。