有几个内置的提供者,比如查询字符串等。这些提供者从链的开始被一个一个地调用,直到它们中的一些能够提供一个值。
因此,您必须通过以下代码行在默认值提供程序链的顶部注册“CustomValueProviderFactory”:
ValueProviderFactories.Factories.Insert(0, new Listhell.CODE.CustomValueProviderFactory());
因此,首先调用您的自定义价值提供者,您应该从任何需要的地方获取价值。以下代码行演示了如何从 cookie 中获取此值:
public class CookieValueProvider: IValueProvider
{
public bool ContainsPrefix(string prefix)
{
return HttpContext.Current.Request.Cookies[prefix] != null;
}
public ValueProviderResult GetValue(string key)
{
if(HttpContext.Current.Request.Cookies[key] == null)
return null;
return new ValueProviderResult(HttpContext.Current.Request.Cookies[key],
HttpContext.Current.Request.Cookies[key].ToString(), CultureInfo.CurrentCulture);
}
}
希望它有所帮助。