-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from billbogaiv/add-header-value-provider
Add Header ValueProvider
- Loading branch information
Showing
6 changed files
with
141 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/HybridModelBinding/ModelBinding/HeaderValueProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Internal; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
|
||
namespace HybridModelBinding.ModelBinding | ||
{ | ||
/// <summary> | ||
/// Modified from https://github.com/aspnet/Mvc/blob/8d66f104f7f2ca42ee8b21f75b0e2b3e1abe2e00/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/QueryStringValueProvider.cs. | ||
/// </summary> | ||
public class HeaderValueProvider : BindingSourceValueProvider, IEnumerableValueProvider | ||
{ | ||
public HeaderValueProvider( | ||
BindingSource bindingSource, | ||
IHeaderDictionary values, | ||
CultureInfo culture) | ||
: base(bindingSource) | ||
{ | ||
if (bindingSource == null) | ||
{ | ||
throw new ArgumentNullException(nameof(bindingSource)); | ||
} | ||
|
||
this.values = values ?? throw new ArgumentNullException(nameof(values)); | ||
Culture = culture; | ||
} | ||
|
||
public CultureInfo Culture { get; private set; } | ||
|
||
private readonly IHeaderDictionary values; | ||
private PrefixContainer prefixContainer; | ||
|
||
protected PrefixContainer PrefixContainer | ||
{ | ||
get | ||
{ | ||
if (prefixContainer == null) | ||
{ | ||
prefixContainer = new PrefixContainer(values.Keys); | ||
} | ||
|
||
return prefixContainer; | ||
} | ||
} | ||
|
||
public override bool ContainsPrefix(string prefix) | ||
{ | ||
return PrefixContainer.ContainsPrefix(prefix); | ||
} | ||
|
||
public virtual IDictionary<string, string> GetKeysFromPrefix(string prefix) | ||
{ | ||
if (prefix == null) | ||
{ | ||
throw new ArgumentNullException(nameof(prefix)); | ||
} | ||
|
||
return PrefixContainer.GetKeysFromPrefix(prefix); | ||
} | ||
|
||
public override ValueProviderResult GetValue(string key) | ||
{ | ||
if (key == null) | ||
{ | ||
throw new ArgumentNullException(nameof(key)); | ||
} | ||
|
||
var values = this.values[key]; | ||
|
||
if (values.Count == 0) | ||
{ | ||
return ValueProviderResult.None; | ||
} | ||
else | ||
{ | ||
return new ValueProviderResult(values, Culture); | ||
} | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/HybridModelBinding/ModelBinding/HeaderValueProviderFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters