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
Consider adding Text overloads to support UTF8 text #41919
Comments
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
Does ReadOnlyMemory<byte> foo = "foo"u8; work without intermediate |
I believe that allocates but @stephentoub would know. |
That won't compile as of the latest language design and compiler version. You'd need to write: ReadOnlyMemory<byte> foo = "foo"u8.ToArray(); which does of course allocate. The code you wrote with the existing compiler allocates as well. |
Hmm I need to get an updated compiler.. |
Do we expect people to use pooled |
API review notes:
|
The natural type of "..."u8 is |
If we're going to have to copy either way which it seems like we will because there's no way for the developer returning |
@halter73 Sure. |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
API Review:
Api approved! namespace Microsoft.AspNetCore.Http;
public static class Results
{
+ public static IResult Text(ReadOnlySpan<byte> utf8Content, string? contentType = null, int? statusCode = null)
}
public static class TypedResults
{
+ public static ContentHttpResult Text(ReadOnlySpan<byte> utf8Content, string? contentType = null, int? statusCode = null);
} |
Almost. We don't want to convert to a String here. @halter73 We missed an API (my bad) there should also be a new public sealed partial class Utf8ContentHttpResult : IResult
{
public Memory<byte> ResponseContent { get; internal init; }
public string? ContentType { get; internal init; }
public int? StatusCode { get; internal init; }
public Task ExecuteAsync(HttpContext httpContext);
} |
Made a few changes to the API:
namespace Microsoft.AspNetCore.Http;
public static class Results
{
+ public static IResult Text(ReadOnlySpan<byte> utf8Content, string? contentType = null, int? statusCode = null)
}
public static class TypedResults
{
+ public static Utf8ContentHttpResult Text(ReadOnlySpan<byte> utf8Content, string? contentType = null, int? statusCode = null);
}
+public sealed partial class Utf8ContentHttpResult : IResult, IStatusCodeHttpResult, IContentTypeHttpResult
+{
+ public ReadOnlyMemory<byte> ResponseContent { get; internal init; }
+
+ public string? ContentType { get; internal init; }
+
+ public int? StatusCode { get; internal init; }
+
+ int IStatusCodeHttpResult.StatusCode => StatusCode ?? 200;
+
+ public Task ExecuteAsync(HttpContext httpContext);
+} |
@Tarun047 my bad, I forgot you wanted to do this one 😞 |
No worries @davidfowl 😀 |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
|
It could also implement IValueResult (both generic and non generic) but I don’t have a strong opinion here |
OK now it matches what ContentHttpResult would have. |
Is there a particular reason that the |
I think I see why. Should we make |
In my opinion, I don't think we should because the only reason for I did not want to change it in my PR but, in my opinion, |
I think there's a big difference between a result that always sets a 200 OK result vs a result that just skips setting the status code. Certain result types like |
Did we close on this? Should it be nullable or not? |
API Review Notes:
namespace Microsoft.AspNetCore.Http;
public static class Results
{
+ public static IResult Text(ReadOnlySpan<byte> utf8Content, string? contentType = null, int? statusCode = null)
}
public static class TypedResults
{
+ public static Utf8ContentHttpResult Text(ReadOnlySpan<byte> utf8Content, string? contentType = null, int? statusCode = null);
}
+public sealed partial class Utf8ContentHttpResult : IResult, IStatusCodeHttpResult, IContentTypeHttpResult
+{
+ public ReadOnlyMemory<byte> ResponseContent { get; }
+
+ public string? ContentType { get; }
+
+ public int? StatusCode { get; }
+
+ public Task ExecuteAsync(HttpContext httpContext);
+}
public interface IStatusCodeHttpResult
{
- public int StatusCode { get; }
+ public int? StatusCode { get; }
}
// Plus a lot of changes to other types implementing IStatusCodeHttpResult Approved! |
Background and Motivation
We've added the utf8 text literals to C# 11 and it would be nice to add some convenience helpers for producing results with utf8 string content.
Proposed API
Usage Examples
Risks
None
The text was updated successfully, but these errors were encountered: