Fetch Header Information Using CustomValueProvider In ASP.NET Web API

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:

solution

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:
code
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.

code
This is the complete code for the CustomHeaderValueProvider and Custom Factory.

  1. namespace WebApiDemo  
  2. {  
  3.     public class CustomHeaderValueProvider : IValueProvider  
  4.     {  
  5.         #region IValueProvider Members  
  6.   
  7.         public Dictionary<stringstring> objCollection;  
  8.         public CustomHeaderValueProvider(HttpActionContext context)  
  9.         {  
  10.             objCollection = new Dictionary<stringstring>();  
  11.   
  12.             foreach (var item in context.Request.Headers)  
  13.             {  
  14.                 objCollection.Add(item.Key, string.Join(string.Empty,item.Value));  
  15.             }  
  16.         }  
  17.   
  18.         public bool ContainsPrefix(string prefix)  
  19.         {  
  20.             return objCollection.Keys.Contains(prefix);    
  21.         }  
  22.   
  23.         public ValueProviderResult GetValue(string key)  
  24.         {  
  25.             string resultValue;  
  26.             if (key == null)  
  27.                 throw new Exception("NullReferenceException");  
  28.             if(objCollection.TryGetValue(key, out resultValue))  
  29.             {  
  30.                 return new ValueProviderResult(resultValue, resultValue, System.Globalization.CultureInfo.InvariantCulture);  
  31.             }  
  32.             return null;  
  33.         }  
  34.  
  35.         #endregion  
  36.     }  
  37.   
  38.   
  39.     public class CustomValueProviderFactory : ValueProviderFactory  
  40.     {  
  41.   
  42.         public override IValueProvider GetValueProvider(HttpActionContext actionContext)  
  43.         {  
  44.             return new CustomHeaderValueProvider(actionContext);  
  45.         }  
  46.     }  
  47. }  
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:
code

Press F5 and run WebApi application. The following window will appear and ensure that WebApi is running.

application

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.

tool

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:

code

The image shown above apparently states that the value send by the user has fetched and returning by CustomValueProvider.

code

Later it returns HttpResponseException as shown below:

HttpResponseException

This is how we can manage valueProvider for some valuable keys at WebApi HttpPipeLine. Please must read wrapping up section below.

wrap

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:

Up Next
    Ebook Download
    View all
    Learn
    View all