zoukankan      html  css  js  c++  java
  • 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());
                        });
                }

  • 相关阅读:
    Express 2015 RC for Windows 10 安装
    离线下载Express 2015 for Windows 10
    离线下载Express 2015 for Windows 10
    windows 10预览版升级win10 7月29 10240.16384
    windows 10预览版升级win10 7月29 10240.16384
    C# 获得系统环境
    C# 获得系统环境
    C++宽字符串转字符串
    C++宽字符串转字符串
    C++ md5类,封装好
  • 原文地址:https://www.cnblogs.com/liek/p/4624429.html
Copyright © 2011-2022 走看看