Asp.net Vnext IValueProvider
概述
本文已经同步到《Asp.net Vnext 系列教程 》中]
IValueProvider
根据ValueProvider获取数据,在对数据进行绑定
代码实现
private class CustomValueProvider : IValueProvider { //判断否具有指定的前缀 public Task<bool> ContainsPrefixAsync(string prefix) { var result = string.Equals(prefix, "test", StringComparison.OrdinalIgnoreCase); return Task.FromResult(result); } //返回ValueProviderResult public Task<ValueProviderResult> GetValueAsync(string key) { //进行绑定的值 var value = "custom-value-provider-value"; var result = new ValueProviderResult(value, value, CultureInfo.CurrentCulture); return Task.FromResult(result); } }
ValueProvider工厂
public IValueProvider GetValueProvider(ValueProviderFactoryContext context) { if (context.HttpContext.Request.Path.Value.Contains("TestValueProvider")) { return new CustomValueProvider(); } return null; }
控制器
public class HomeController { public string TestValueProvider(string test) { return test; } }
启动类
public class Startup { // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IMemoryCache, MemoryCache>(); services.AddCaching(); services.AddSession(); services.AddMvc() .Configure<MvcOptions>(options => {//添加CustomValueProviderFactory options.ValueProviderFactories.Insert(1, new CustomValueProviderFactory()); }); }