-
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.
New behavior mimics existing ASP.NET MVC behavior of QueryString and Form value providers. Not really sure why this isn't included in that package. Didn't do thorough testing, but did notice one gotcha if identical header-keys are submitted: the values are combined as a single comma-separated string. Implements #23
- Loading branch information
1 parent
b17c978
commit abb7410
Showing
6 changed files
with
139 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); | ||
} | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using System; | ||
using System.Globalization; | ||
using System.Threading.Tasks; | ||
|
||
namespace HybridModelBinding.ModelBinding | ||
{ | ||
public class HeaderValueProviderFactory : IValueProviderFactory | ||
{ | ||
/// <summary> | ||
/// We need to re-create a Header BindingSource since the default ASP.NET MVC version | ||
/// is greedy and that won't work as a value provider. | ||
/// | ||
/// Ref. https://github.com/aspnet/Mvc/blob/8d66f104f7f2ca42ee8b21f75b0e2b3e1abe2e00/src/Microsoft.AspNetCore.Mvc.Abstractions/ModelBinding/BindingSource.cs | ||
/// </summary> | ||
private BindingSource Header = new BindingSource( | ||
BindingSource.Header.Id, | ||
BindingSource.Header.DisplayName, | ||
isGreedy: false, | ||
isFromRequest: BindingSource.Header.IsFromRequest); | ||
|
||
public Task CreateValueProviderAsync(ValueProviderFactoryContext context) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
var headers = context.ActionContext.HttpContext.Request.Headers; | ||
if (headers != null && headers.Count > 0) | ||
{ | ||
var valueProvider = new HeaderValueProvider( | ||
Header, | ||
headers, | ||
CultureInfo.InvariantCulture); | ||
|
||
context.ValueProviders.Add(valueProvider); | ||
} | ||
|
||
#if NET451 | ||
return Task.FromResult(0); | ||
#else | ||
return Task.CompletedTask; | ||
#endif | ||
} | ||
} | ||
} |
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