ASP.NET MVC以ValueProvider為核心的值提供系統 二

來源:互聯網
上載者:User

DictionaryValueProvider

NameValueCollectionValueProvider採用一個NameValueCollection作為資料來源,DictionnaryValueProvider的資料來源類型自然就是一個Dictionnary。NameValueCollection和Dictionnary都是一個索引值對的集合,它們之間的不同之處在NameValueCollection運行元素具有相同的Key,Dictionnary卻要求元素的Key具有唯一性。

一、DictionaryValueProvider<TValue>

DictionnaryValueProvider的類型全名為System.Web.Mvc.DictionaryValueProvider<TValue>,如下面的代碼片斷所示,DictionaryValueProvider<TValue>實現了IEnumerableValueProvider和IValueProvider介面,建構函式接受一個IDictionary<string, TValue>對象,該對象表示作為資料來源的字典。定義在DictionaryValueProvider<TValue>中所有方法的邏輯與定義在NameValueCollectionValueProvider中的同名方法並沒有本質區別。

   1: public class DictionaryValueProvider<TValue> : IEnumerableValueProvider, IValueProvider
2: {
3: public DictionaryValueProvider(IDictionary<string, TValue> dictionary, CultureInfo culture);
4: public virtual bool ContainsPrefix(string prefix);
5: public virtual IDictionary<string, string> GetKeysFromPrefix(string prefix);
6: public virtual ValueProviderResult GetValue(string key);
7: }

二、RouteDataValueProvider

將當前路由資料作為資料來源的RouteDataValueProvider繼承自DictionaryValueProvider<TValue>。如下面的代碼片斷所示,基於當前Controller上下文構建的RouteDataValueProvider直接將表示當前路由資料的RouteData對象的Values屬性(這是一個RouteValueDictionary對象)作為資料來源。

   1: public sealed class RouteDataValueProvider : DictionaryValueProvider<object>
2: {
3: public RouteDataValueProvider(ControllerContext controllerContext) :
4: base(controllerContext.RouteData.Values, CultureInfo.InvariantCulture)
5: {
6: }
7: }

三、HttpFileCollectionValueProvider

我們可以通過類型為file的輸入元素進行檔案的上傳,在表示HTTP請求的HttpRequestBase對象中,上傳檔案通過唯讀屬性Files表示。從下面的代碼片斷所示,該屬性類型為HttpFileCollectionBase,是一個元素類型為HttpPostedFileBase的集合。

   1: public abstract class HttpRequestBase
2: {
3: public virtual HttpFileCollectionBase Files { get; }
4: }
5: public abstract class HttpFileCollectionBase : NameObjectCollectionBase, ICollection, IEnumerable
6: {
7: public virtual string[] AllKeys { get; }
8: public override int Count { get; }
9: public virtual HttpPostedFileBase this[int index] { get; }
10: public virtual HttpPostedFileBase this[string name] { get; }
11: }
12: public abstract class HttpPostedFileBase
13: {
14: public virtual void SaveAs(string filename);
15:
16: public virtual int ContentLength { get; }
17: public virtual string ContentType { get; }
18: public virtual string FileName { get; }
19: public virtual Stream InputStream { get; }
20: }

用於處理上傳檔案的Action方法通常定義類型為HttpPostedFileBase及其列表的參數來表示上傳的檔案,針對HttpPostedFileBase參數的Model綁定選用的資料就來源於表示當前請求的HttpRequestBase的Files屬性,而具體參數值的提供最終通過具有如下定義的HttpFileCollectionValueProvider來實現。

   1: public sealed class HttpFileCollectionValueProvider : DictionaryValueProvider<HttpPostedFileBase[]>
2: {
3: public HttpFileCollectionValueProvider(ControllerContext controllerContext);
4: }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.