Skip to content

Introducing StringValues to replace string[] #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Tratcher opened this issue Aug 29, 2015 · 0 comments
Open

Introducing StringValues to replace string[] #60

Tratcher opened this issue Aug 29, 2015 · 0 comments

Comments

@Tratcher
Copy link
Member

A new struct Microsoft.Framework.Primitives.StringValues has been introduced to streamline handling of values that may be empty, single strings, or multiple strings. The value is implicitly convertable to and from string and string[], and also provides helpers like Concat and IsNullOrEmpty.

https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.Framework.Primitives/StringValues.cs

The type is now used in request and response headers, query values, and form values. As such there have been some API changes in IHeaderDictionary and IReadableStringCollection (used in Form and Query). The indexer is now the primary API for these collections since the presence and plurality are taken care of by StringValues.

Examples:

string combineValue = httpContext.Request.Headers["header1];
if (string.IsNullOrEmpty(combineValue)) // ...
var values = httpContext.Request.Headers["header1"];
if (StringValues.IsNullOrEmpty(values)) // ...
httpContext.Response.Headers["CustomHeader1"] = "singleValue";
httpContext.Response.Headers["CustomHeader2"] =  new[] { "firstValue", "secondValue" };

Also note that some APIs from IHeaderDictionary have been moved to extension methods in the Microsoft.AspNet.Http.Extensions package. See https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNet.Http.Extensions/HeaderDictionaryExtensions.cs

See aspnet/HttpAbstractions#382 for discussion.

@Tratcher Tratcher added this to the 1.0.0-beta8 milestone Aug 29, 2015
@Tratcher Tratcher assigned Tratcher and unassigned Tratcher Aug 29, 2015
@aspnet aspnet locked and limited conversation to collaborators Aug 29, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant