Refer to my previous article on Value providers in MVC: Custom Value Providers in MVC. Value Providers are the components that feed data to model binders. Feeding the data means installing the data to the Model binder for further use at the action level.
The framework contains a few built-in value providers named FormValueProvider, RouteDataValueProvider, QueryStringValueProvider and HttpFileCollectionValueProvider that fetch data from Request.Form, Request.QueryString, Request.Files and RouteData.Values.
An idea is very simple as the name implies; Value providers means an agent provides some value for its uses at action level.
This is a solution structure which has CustomHeaderValueProvider as depicted below:
Here is an easy way to create Custom Value Providers.
Each value provider implements an interface IValueProvider that has two methods as shown below in the image:
The ContainsPrefix method is called by the model binder to determine whether the value provider has the data for a given prefix. The GetValue method returns a value for a given data key or returns null if the provider doesn't have any suitable data
After this time to register Value Provider through factory. I’ve created a factory CustomValueProviderFactory to register our CustomHeaderValueProvider as shown in image below. This class has one method which returns calls and returns CustomheaderValueProvider after managing its stuff.
This is the complete code for the CustomHeaderValueProvider and Custom Factory.
- namespace WebApiDemo
- {
- public class CustomHeaderValueProvider : IValueProvider
- {
- #region IValueProvider Members
-
- public Dictionary<string, string> objCollection;
- public CustomHeaderValueProvider(HttpActionContext context)
- {
- objCollection = new Dictionary<string, string>();
-
- foreach (var item in context.Request.Headers)
- {
- objCollection.Add(item.Key, string.Join(string.Empty,item.Value));
- }
- }
-
- public bool ContainsPrefix(string prefix)
- {
- return objCollection.Keys.Contains(prefix);
- }
-
- public ValueProviderResult GetValue(string key)
- {
- string resultValue;
- if (key == null)
- throw new Exception("NullReferenceException");
- if(objCollection.TryGetValue(key, out resultValue))
- {
- return new ValueProviderResult(resultValue, resultValue, System.Globalization.CultureInfo.InvariantCulture);
- }
- return null;
- }
-
- #endregion
- }
-
-
- public class CustomValueProviderFactory : ValueProviderFactory
- {
-
- public override IValueProvider GetValueProvider(HttpActionContext actionContext)
- {
- return new CustomHeaderValueProvider(actionContext);
- }
- }
- }
In the above code you will have noticed the constructor of Value Provider class which collects header information and save it to dictionary item for further uses also shown in image below:
Press F5 and run WebApi application. The following window will appear and ensure that WebApi is running.
Send the Get request in order to verify the execution cycle as given below. Copy and paste the following Url (http://localhost:57888/api/employees/GetEmp/5) in PostMan tool as depicted below in screen shot.
As soon as you click on the send button it reaches to Custom value provider’s method to execute set of statement .Please have a look at the images shown below:
The image shown above apparently states that the value send by the user has fetched and returning by CustomValueProvider.
Later it returns HttpResponseException as shown below:
This is how we can manage valueProvider for some valuable keys at WebApi HttpPipeLine. Please must read wrapping up section below.
At the time of the model binding the DefaultModelBinder checks with the value providers to determine if they can return a value for the parameter Id by calling the ContainsPrefix method. If none of the value providers registered can return then it checks through CustomtvalueProvider whether such a parameter is stored and if yes it returns the value.
Read more articles on ASP.NET: