Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Adding TextPlainFormatter to always handle returning strings as text\pla... #868

Closed
wants to merge 3 commits into from

Conversation

harshgMSFT
Copy link
Contributor

One Functional test will be added after #849

@@ -33,6 +33,7 @@ public void Setup(MvcOptions options)
options.ModelBinders.Add(new ComplexModelDtoModelBinder());

// Set up default output formatters.
options.OutputFormatters.Add(new TextPlainFormatter());
options.OutputFormatters.Add(new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), true));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm..looks like my comment in other PR didn't make it...named parameter and also indent should be false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my bad- actually I had to base it off another PR ( the actual code in the repo has the correct value https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs)

@harshgMSFT
Copy link
Contributor Author

Branch updated.

return true;
}

return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add support for StringBuilder and StringWriter and open a issue to followup @pranavkm when he is done with the RazorTextWriter to see if we think it's useful here as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next PR>

@yishaigalatzer
Copy link
Contributor

:shipit: I'm ok with the one minor casting change to push in, and followup with the stringbuilder support in another PR to reduce the clutter.

Hmm I take it back, there are essential pieces missing here

@yishaigalatzer
Copy link
Contributor

@RickStrahl this I think is a nice way to address your feedback in #629. It's easy enough to opt out of this behavior by simply removing this default formatter (or customizing it)

/// </summary>
public class TextPlainFormatter : IOutputFormatter
{
public bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it virtual, so it's easy to understand how to override the content type handling behavior

@yishaigalatzer
Copy link
Contributor

This PR should also include the change removing the string handling from the object content result

@harshgMSFT
Copy link
Contributor Author

The object result pr does remove that. I was planning to push this in post the changes of object result.

@harshgMSFT
Copy link
Contributor Author

Branch Updated.

using (var writer = new StreamWriter(response.Body, context.SelectedEncoding, 1024, leaveOpen: true))
{
await writer.WriteAsync(valueAsString);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of things to note here:

  1. When the value is null, I am not sure if we should be explicitly setting the Content-Length header to 0 as otherwise the host layers might think that you are sending a chunked response...
  2. In Web API, we prevent double buffering for scenarios where we can already calculate the length of the response content and as part of this StringContent is not buffered again as we can calculate the length of it...so need to see how its going to be in our case...

yeah, both these above points depend upon the response buffering story that is yet to be decided i think..

/cc: @yishaigalatzer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the first one is certainly doable even if we don't do buffering.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open up a followup issue for setting content length in responses. I think the answer is that we should not set it, as it depends heavily on the encoding.

@Tratcher got guidance

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting Content-Length is strongly preferred to chunked, if you can calculate it with a reasonable amount of effort. In this case, consider calling context.SelectedEncoding.GetByteCount and see how that affects your perf. The result will vary widely depending on your selected encoding (ASCII = constant, UTF-8 = variable).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets follow up on #919

namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Writes a string value to the response.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always writes a string value to the response, regardless of requested contenttype

@yishaigalatzer
Copy link
Contributor

Enable the tests for string

@harshgMSFT harshgMSFT closed this Aug 1, 2014
@harshgMSFT harshgMSFT deleted the TextPlainFormatter branch August 1, 2014 20:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants