在过去的几个小时里,我一直在努力解决这个问题。我所要做的就是从当前路径中选择动作参数,就像这样。此方法驻留在一个简单的静态助手类中。
public static string GetStateName(ActionExecutingContext filterContext)
{
var stateParam = filterContext.ActionParameters.Where(p => p.Key == RouteConst.StateName).FirstOrDefault();
return !string.IsNullOrEmpty(stateParam.Key) ? stateParam.Value.ToType<string>() : string.Empty;
}
更新
:根据Necros的建议,这就是我最后所做的。
{
public static string GetMarketName(this ActionExecutingContext filterContext)
{return GetActionParamValue(filterContext, RouteConst.MarketName).ToType<string>();}
public static string GetStateName(this ActionExecutingContext filterContext)
{return GetActionParamValue(filterContext, RouteConst.StateName).ToType<string>();}
private static string GetActionParamValue(ActionExecutingContext filterContext, string actionParamName)
{
var actionParam = filterContext.ActionParameters.Where(p => p.Key == actionParamName).FirstOrDefault();
return !string.IsNullOrEmpty(actionParam.Key) ? actionParam.Value.ToType<string>() : string.Empty;
}
ToType()是另一个在内部使用Convert.ChangeType(value,type)的扩展方法。