mvc action 參數綁定——值提供器【學習筆記】

來源:互聯網
上載者:User

標籤:style   http   color   使用   os   io   資料   for   

每次http請求的各種資料(表單資料、url的資料、路由資料等等)都儲存在不同的IValueProvider介面的實作類別中。

而IValueProvider介面的實作類別是通過ValueProviderFactory建立的。

在mvc中原生的ValueProviderFactory有六種:

  • ChildActionValueProviderFactory:根據給定的Controller上下文建立一個ChildActionValueProvider對象。
  • FormValueProviderFactory:根據給定的Controller上下文建立一個FormValueProvider對象。
  • JsonValueProviderFactory:將以JSON形式表示的請求資料轉換成一個Dictionary<string, object>對象,並最終建立一個DictionaryValueProvider<object>對象。
  • RouteDataValueProviderFactory:根據給定的Controller上下文建立一個RouteDataValueProvider對象。
  • QueryStringValueProviderFactory:根據給定的Controller上下文建立一個QueryStringValueProvider對象。
  • HttpFileCollectionValueProviderFactory:根據給定的Controller上下文建立一個HttpFileCollectionValueProvider對象。

 

 

ValueProviderFactory需要註冊到ValueProviderFactories的靜態屬性Factories中,才能被使用。

   1: public static class ValueProviderFactories
   2: {
   3:     public static ValueProviderFactoryCollection Factories { get; }
   4: }
   6: public class ValueProviderFactoryCollection : Collection<ValueProviderFactory>
   7: { 
   8:     public ValueProviderFactoryCollection();
   9:     public ValueProviderFactoryCollection(IList<ValueProviderFactory> list);    
  10:     public IValueProvider GetValueProvider(ControllerContext controllerContext);
  11: }


系統首先通過ValueProviderFactories類的靜態屬性獲得ValueProviderFactoryCollection ,然後通過GetValueProvider方法獲得一個ValueProviderCollection,ValueProviderCollection就是值提供器集合。

   1: public class ValueProviderCollection : Collection<IValueProvider>, IUnvalidatedValueProvider, IEnumerableValueProvider, IValueProvider
   2: {
   3:     public ValueProviderCollection();
   4:     public ValueProviderCollection(IList<IValueProvider> list);
   6:     public virtual bool ContainsPrefix(string prefix);
   7:     public virtual IDictionary<string, string> GetKeysFromPrefix(string prefix); 
   8:     public virtual ValueProviderResult GetValue(string key);
   9:     public virtual ValueProviderResult GetValue(string key, bool skipValidation);   
  10: }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.