Created
July 18, 2019 21:21
-
-
Save musukvl/dffe0e0c55006d30fa570e394fb3027e to your computer and use it in GitHub Desktop.
Example rendering IEnumerable to Asp.net core API action result
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
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
namespace ApiModelingApp.Controllers | |
{ | |
[Route("api/generation")] | |
[ApiController] | |
public class GenerationController : ControllerBase | |
{ | |
// GET api/values | |
[HttpGet] | |
[Route("text")] | |
public ActionResult Get(long paragraphs) | |
{ | |
var ie = GenerateParagraph(paragraphs); | |
//return Content(string.Join(Environment.NewLine, ie.ToArray())); | |
return new EnumerableStreamResult(ie, "text/plain"); | |
} | |
private Random _random = new Random(); | |
private string[] _loremIpsumWords; | |
public GenerationController() | |
{ | |
_loremIpsumWords = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum" | |
.Split(' '); | |
} | |
private string GenerateParagraph(int wordsCount) | |
{ | |
List<string> words = new List<string>(); | |
for (int i = 0; i < wordsCount; i++) | |
{ | |
words.Add(_loremIpsumWords[_random.Next(_loremIpsumWords.Length)]); | |
} | |
return string.Join(" ", words) + Environment.NewLine; | |
} | |
private IEnumerable<string> GenerateParagraph(long count) | |
{ | |
for (var i = 0; i < count; i++) | |
{ | |
var par = GenerateParagraph(_random.Next(100)); | |
yield return par; | |
} | |
} | |
} | |
public class EnumerableStreamResult : FileResult | |
{ | |
public IEnumerable<string> Enumerable | |
{ | |
get; | |
private set; | |
} | |
public Encoding ContentEncoding | |
{ | |
get; | |
set; | |
} | |
public EnumerableStreamResult(IEnumerable<string> enumerable, string contentType) | |
: base(contentType) | |
{ | |
if (enumerable == null) | |
{ | |
throw new ArgumentNullException("enumerable"); | |
} | |
this.Enumerable = enumerable; | |
} | |
public override async Task ExecuteResultAsync(ActionContext context) | |
{ | |
var response = context.HttpContext.Response; | |
response.ContentType = ContentType; | |
foreach (var item in Enumerable) | |
await context.HttpContext.Response.Body // is System.IO.Stream | |
.WriteAsync(Encoding.UTF8.GetBytes(item)) | |
.ConfigureAwait(false); | |
await base.ExecuteResultAsync(context).ConfigureAwait(false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment