SanSYS

Untitled

Jun 14th, 2015
24,902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using Microsoft.AspNet.Mvc.ModelBinding;
  2. using System;
  3. using System.Threading.Tasks;
  4.  
  5. namespace TestDependencyInjection.Lib
  6. {
  7.     public class SomeProviderModelBinder : IModelBinder
  8.     {
  9.         public Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext)
  10.         {
  11.             try
  12.             {
  13.                 object someProvider = bindingContext.OperationBindingContext.HttpContext.ApplicationServices.GetService(bindingContext.ModelType);
  14.  
  15.                 if (someProvider != null)
  16.                 {
  17.                     return Task.FromResult(new ModelBindingResult(someProvider, bindingContext.ModelName, true));
  18.                 }
  19.             }
  20.             catch (Exception ex)
  21.             {
  22.                 //LogException(ex);
  23.             }
  24.  
  25.             return Task.FromResult<ModelBindingResult>(null);
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment