asp.net MVC provides a system with Valueprovider as its core value

Source: Internet
Author: User
Tags bool constructor

Namevaluecollectionvalueprovider

During the model binding process, you need to extract the corresponding data from the request data based on the binding context based on the action method parameter to provide the appropriate data. Specifically, the model-bound data has multiple sources, possibly from a post form or JSON string, or from the current routing data, or from the SUSSU string of the request address. asp.net MVC implements this data acquisition/delivery mechanism based on different data sources in a component called Valueprovider.

First, Ivalueprovider and Valueproviderresult

Generally speaking, a data source used by a valueprovider is a dictionary-type data structure from which we get a key that matches the current binding context. Valueprovider implements the interface Ivalueprovider,getvalue method with the following definition to obtain the corresponding value object from the data source based on the specified key, which is based on the current binding context. This key may not be exactly the same as the key that exists in the corresponding data entry in the data source, which may add a corresponding prefix based on the former, and the Containsprefix method is used to determine whether the key of the data source dictionary has the specified prefix.

   1:public Interface Ivalueprovider
2: {
3: bool Containsprefix (string prefix);
4: valueproviderresult GetValue (string key);
5:}

Ivalueprovider's GetValue Returns a Valueproviderresult object, and we can consider valueproviderresult as an encapsulation of the object that the Valueprovider provides. As shown in the following code fragment, Valueproviderresult has three read-only properties, where RawValue represents the original value object. Attemptedvalue represents the string representation of a value object, which is used primarily for display.

   1: [Serializable]
2:public class Valueproviderresult
3: {
4: Public Valueproviderresult (object RawValue, String attemptedvalue, CultureInfo culture);
5: Public object ConvertTo (type type);
6: Public Virtual Object ConvertTo (type type, CultureInfo culture);
7:
8: Public string Attemptedvalue {get;}
9: Public CultureInfo Culture {get;}
: Public object RawValue {get;}
11:}

Valueproviderresult provides two ConvertTo method overloads to implement conversions to the specified target type. Some types of formatting behavior depend on the corresponding language culture (such as time, date, currency, etc.), and the auxiliary format culture information of the lake is represented by the culture attribute. One of the first Valueproviderresult method overloads is the type conversion of the language culture represented by the attribute culture.

Second, Namevaluecollectionvalueprovider

As already mentioned, the model data source generally has a dictionary-like structure, and namevaluecollection can be represented as a dictionary with key not unique. The Valueprovider of the NameValueCollection object as a data source is represented by a namevaluecollectionvalueprovider type that has the following definitions. The NameValueCollection object representing the data source is specified in the constructor, and the parameters of another CultureInfo type of the constructor represent the language and cultural information that serves the data transformation.

 1:public class Namevaluecollectionvalueprovider:iunvalidatedvalueprovider, Ienumerablevalueprovider, IValuePr   Ovider 
2: {
3://Other Members
4:public Namevaluecollectionvalueprovider (NameValueCollection collection, CultureInfo culture);
5:
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:}
11: 
12:public interface Ienumerablevalueprovider:ivalueprovider
: {
14:idictionary< ; string, string> getkeysfromprefix (string prefix);
15:}
16: 
17:public interface Iunvalidatedvalueprovider:ivalueprovider
: {
19:valueprovider Result GetValue (string key, bool skipvalidation);
:}

From the code snippet above, we can see that, in addition to the Ivalueprovider interface, Namevaluecollectionvalueprovider also implements the Ienumerablevalueprovider and Iunvalidatedvalueprovider two interfaces. As the name suggests, Ienumerablevalueprovider is primarily used to provide data for a collection of target types, and the method Getkeysfromprefix returns a key with the specified prefix as a dictionary. By default, data is validated while data is provided, and the Iunvalidatedvalueprovider interface provides an additional GetValue method that allows us to ignore validation of the data.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.