Skip to content

Instantly share code, notes, and snippets.

@paulduran
Created May 24, 2012 05:18
Show Gist options
  • Save paulduran/2779611 to your computer and use it in GitHub Desktop.
Save paulduran/2779611 to your computer and use it in GitHub Desktop.
Creating Reversing Value Provider
// in global.asax
protected void Application_Start()
{
// other stuff
ValueProviderFactories.Factories.Add(new ReverseValueProviderFactory());
}
public class ReverseValueProviderFactory : ValueProviderFactory
{
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
{
return new ReverseValueProvider(
from vpv in ValueProviderFactories.Factories
where vpv.GetType() != typeof(ReverseValueProviderFactory)
select vpv.GetValueProvider(controllerContext));
}
}
public class ReverseValueProvider : IValueProvider
{
private readonly IEnumerable<IValueProvider> valueProviders;
public ReverseValueProvider(IEnumerable<IValueProvider> valueProviders)
{
this.valueProviders = valueProviders;
}
public bool ContainsPrefix(string prefix)
{
return true;
}
public ValueProviderResult GetValue(string key)
{
foreach (IValueProvider valueProvider in valueProviders)
{
if (valueProvider != null)
{
ValueProviderResult result = valueProvider.GetValue(key + "Reversed");
if (result != null)
{
object newRaw = result.RawValue;
if( result.RawValue is string[])
{
newRaw = ((string[]) result.RawValue).Select(Reverse).ToArray();
} else if(result.RawValue is string)
newRaw = Reverse((string) result.RawValue);
var providerResult = new ValueProviderResult(newRaw, Reverse(result.AttemptedValue), result.Culture);
return providerResult;
}
}
}
return null;
}
private string Reverse(string str)
{
return new string(str.Reverse().ToArray());
}
}
public class MyController : Controller
{
// requesting /My/Reverse?nameReversed=luaP => Paul
public ActionResult Reverse(string name)
{
return Content(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment