コード例 #1
0
    public async Task ContentResult_WritesDataCorrectly_ForDifferentContentSizes(string content, string contentType)
    {
        // Arrange
        var contentResult = new ContentResult
        {
            Content     = content,
            ContentType = contentType
        };
        var httpContext  = GetHttpContext();
        var memoryStream = new MemoryStream();

        httpContext.Response.Body = memoryStream;
        var actionContext = GetActionContext(httpContext);
        var encoding      = MediaTypeHeaderValue.Parse(contentType).Encoding;

        // Act
        await contentResult.ExecuteResultAsync(actionContext);

        // Assert
        memoryStream.Seek(0, SeekOrigin.Begin);
        var streamReader  = new StreamReader(memoryStream, encoding);
        var actualContent = await streamReader.ReadToEndAsync();

        Assert.Equal(content, actualContent);
    }
コード例 #2
0
    public async Task ContentResult_ExecuteResultAsync_SetContentTypeAndEncoding_OnResponse(
        MediaTypeHeaderValue contentType,
        string content,
        string responseContentType,
        string expectedContentType,
        byte[] expectedContentData)
    {
        // Arrange
        var contentResult = new ContentResult
        {
            Content     = content,
            ContentType = contentType?.ToString()
        };
        var httpContext  = GetHttpContext();
        var memoryStream = new MemoryStream();

        httpContext.Response.Body        = memoryStream;
        httpContext.Response.ContentType = responseContentType;
        var actionContext = GetActionContext(httpContext);

        // Act
        await contentResult.ExecuteResultAsync(actionContext);

        // Assert
        var finalResponseContentType = httpContext.Response.ContentType;

        Assert.Equal(expectedContentType, finalResponseContentType);
        Assert.Equal(expectedContentData, memoryStream.ToArray());
        Assert.Equal(expectedContentData.Length, httpContext.Response.ContentLength);
    }
コード例 #3
0
        /// <summary>
        /// Responds to the request and executes the action.
        /// </summary>
        /// <param name="statusCode">The status code.</param>
        /// <param name="message">The message.</param>
        /// <returns>HttpResponseMessage.</returns>
        public static Task ExecuteWithAsync(this ActionContext actionContext, int statusCode, string message, string contentType = "text/plain")
        {
            var contentResult = new ContentResult()
            {
                StatusCode  = (int)statusCode,
                ContentType = contentType,
                Content     = message
            };

            return(contentResult.ExecuteResultAsync(actionContext));
        }
コード例 #4
0
        public override Task ExecuteResultAsync(ActionContext context)
        {
            MediaTypeHeaderValue contentType1 = MediaTypeHeaderValue.Parse((StringSegment)"application/json");

            contentType1.Encoding = Encoding.UTF8;
            var content = new ContentResult {
                Content     = _jsonValue,
                ContentType = contentType1?.ToString(),
                StatusCode  = 200
            };

            return(content.ExecuteResultAsync(context));
        }
コード例 #5
0
        private async Task InvokeHtml(HttpContext context, Exception ex)
        {
            var htmlContent = CreateHtml(ex);
            var result      = new ContentResult();

            result.Content     = htmlContent;
            result.StatusCode  = (int)HttpStatusCode.InternalServerError;
            result.ContentType = "text/html; charset=utf-8";

            var routeData        = context.GetRouteData();
            var actionDescriptor = new ActionDescriptor();
            var actionContext    = new ActionContext(context, routeData, actionDescriptor);
            await result.ExecuteResultAsync(actionContext);
        }
コード例 #6
0
        /// <summary>
        /// When the programmer hasn't configured an authentication provider,
        /// provide a clear error message.
        /// </summary>
        Task RequireAuthenticationProviderHandler(HttpContext context)
        {
            var result = new ContentResult()
            {
                Content = "You must configure an authentication "
                          + "provider."
                          + "\nSee README.md in the project source directory.",
                ContentType = "text/plain",
                StatusCode  = 500
            };

            return(result.ExecuteResultAsync(new ActionContext()
            {
                HttpContext = context
            }));
        }
コード例 #7
0
        public async Task ExecuteResultAsync(ActionContext context)
        {
            if (context is null)
            {
                throw new System.ArgumentNullException(nameof(context));
            }

            var renderService = context.HttpContext.RequestServices.GetRequiredService <IRenderService>();
            var renderResult  = await renderService.RenderAsync(_url, _data);

            var contentResult = new ContentResult
            {
                Content     = renderResult,
                ContentType = "text/html"
            };
            await contentResult.ExecuteResultAsync(context);
        }
コード例 #8
0
        public override Task OnExceptionAsync(ExceptionContext context)
        {
            var httpContext = context.HttpContext;
            var headers     = httpContext.Request.Headers;
            var mustPublish = headers.TryGetValue(FusionHeaders.RequestPublication, out var _);

            if (!mustPublish)
            {
                return(base.OnExceptionAsync(context));
            }

            var result = new ContentResult()
            {
                StatusCode = (int)HttpStatusCode.InternalServerError,
                Content    = context.Exception.Message,
            };

            return(result.ExecuteResultAsync(context));
        }
コード例 #9
0
    public async Task ContentResult_ExecuteResultAsync_Response_NullContent_SetsContentTypeAndEncoding()
    {
        // Arrange
        var contentResult = new ContentResult
        {
            Content     = null,
            ContentType = new MediaTypeHeaderValue("text/plain")
            {
                Encoding = Encoding.Unicode
            }.ToString()
        };
        var httpContext   = GetHttpContext();
        var actionContext = GetActionContext(httpContext);

        // Act
        await contentResult.ExecuteResultAsync(actionContext);

        // Assert
        MediaTypeAssert.Equal("text/plain; charset=utf-16", httpContext.Response.ContentType);
    }
コード例 #10
0
        async Task ProcessDocumentAsync(object model, ActionContext context)
        {
            var prerenderer = context.HttpContext.RequestServices.GetService(typeof(ISpaPrerenderer)) as ISpaPrerenderer;
            var result      = await prerenderer.RenderToString("wwwroot/dist/kasbah-server", customDataParameter : _model);

            if (!string.IsNullOrEmpty(result.RedirectUrl))
            {
                await new RedirectResult(result.RedirectUrl).ExecuteResultAsync(context);
            }
            else
            {
                var res = new ContentResult
                {
                    Content     = $"<!DOCTYPE html>{result.Html}",
                    ContentType = "text/html"
                };

                await res.ExecuteResultAsync(context);
            }
        }
コード例 #11
0
        public override Task OnExceptionAsync(ExceptionContext context)
        {
            var exception = context.Exception;

            if (RewriteErrors)
            {
                var rewriter = context.HttpContext.RequestServices.GetRequiredService <IErrorRewriter>();
                exception = rewriter.Rewrite(context, exception, true);
            }
            var serializer = new JsonNetSerializer(JsonNetSerializer.DefaultSettings);
            var content    = serializer.Serialize(exception);
            var result     = new ContentResult()
            {
                Content     = content,
                ContentType = "application/json",
                StatusCode  = (int)HttpStatusCode.InternalServerError,
            };

            context.ExceptionHandled = true;
            return(result.ExecuteResultAsync(context));
        }