Beispiel #1
0
        /// <inheritdoc />
        public override async Task ExecuteResultAsync([NotNull] ActionContext context)
        {
            var viewEngine = ViewEngine ??
                             context.HttpContext.RequestServices.GetRequiredService <ICompositeViewEngine>();

            var logger = context.HttpContext.RequestServices.GetRequiredService <ILogger <PartialViewResult> >();

            var options = context.HttpContext.RequestServices.GetRequiredService <IOptions <MvcViewOptions> >();

            var viewName         = ViewName ?? context.ActionDescriptor.Name;
            var viewEngineResult = viewEngine.FindPartialView(context, viewName);

            if (!viewEngineResult.Success)
            {
                logger.LogError(
                    "The partial view '{PartialViewName}' was not found. Searched locations: {SearchedViewLocations}",
                    viewName,
                    viewEngineResult.SearchedLocations);
            }

            var view = viewEngineResult.EnsureSuccessful().View;

            logger.LogVerbose("The partial view '{PartialViewName}' was found.", viewName);

            if (StatusCode != null)
            {
                context.HttpContext.Response.StatusCode = StatusCode.Value;
            }

            using (view as IDisposable)
            {
                await ViewExecutor.ExecuteAsync(
                    view,
                    context,
                    ViewData,
                    TempData,
                    options.Options.HtmlHelperOptions,
                    ContentType);
            }
        }
        public async Task ExecuteAsync_SetsContentTypeAndEncoding(
            MediaTypeHeaderValue contentType,
            string expectedContentType,
            byte[] expectedContentData)
        {
            // Arrange
            var view = new Mock <IView>();

            view.Setup(v => v.RenderAsync(It.IsAny <ViewContext>()))
            .Callback((ViewContext v) =>
            {
                view.ToString();
                v.Writer.Write("abcd");
            })
            .Returns(Task.FromResult(0));

            var context      = new DefaultHttpContext();
            var memoryStream = new MemoryStream();

            context.Response.Body = memoryStream;

            var actionContext = new ActionContext(context,
                                                  new RouteData(),
                                                  new ActionDescriptor());
            var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());

            // Act
            await ViewExecutor.ExecuteAsync(
                view.Object,
                actionContext,
                viewData,
                null,
                new HtmlHelperOptions(),
                contentType);

            // Assert
            Assert.Equal(expectedContentType, context.Response.ContentType);
            Assert.Equal(expectedContentData, memoryStream.ToArray());
        }
        public async Task ExecuteAsync_DoesNotWriteToResponse_OnceExceptionIsThrown()
        {
            // Arrange
            var expectedLength = 0;

            var view = new Mock <IView>();

            view.Setup(v => v.RenderAsync(It.IsAny <ViewContext>()))
            .Callback((ViewContext v) =>
            {
                throw new Exception();
            });

            var context      = new DefaultHttpContext();
            var memoryStream = new MemoryStream();

            context.Response.Body = memoryStream;

            var actionContext = new ActionContext(context,
                                                  new RouteData(),
                                                  new ActionDescriptor());
            var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());

            // Act
            await Record.ExceptionAsync(
                () => ViewExecutor.ExecuteAsync(
                    view.Object,
                    actionContext,
                    viewData,
                    null,
                    new HtmlHelperOptions(),
                    contentType: null));

            // Assert
            Assert.Equal(expectedLength, memoryStream.Length);
        }
Beispiel #4
0
        /// <inheritdoc />
        public override async Task ExecuteResultAsync(ActionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var services   = context.HttpContext.RequestServices;
            var viewEngine = ViewEngine ?? services.GetRequiredService <ICompositeViewEngine>();

            var logger    = services.GetRequiredService <ILogger <ViewResult> >();
            var telemetry = services.GetRequiredService <TelemetrySource>();

            var options = services.GetRequiredService <IOptions <MvcViewOptions> >();

            var viewName         = ViewName ?? context.ActionDescriptor.Name;
            var viewEngineResult = viewEngine.FindView(context, viewName);

            if (!viewEngineResult.Success)
            {
                if (telemetry.IsEnabled("Microsoft.AspNet.Mvc.ViewResultViewNotFound"))
                {
                    telemetry.WriteTelemetry(
                        "Microsoft.AspNet.Mvc.ViewResultViewNotFound",
                        new
                    {
                        actionContext     = context,
                        result            = this,
                        viewName          = viewName,
                        searchedLocations = viewEngineResult.SearchedLocations
                    });
                }

                logger.LogError(
                    "The view '{ViewName}' was not found. Searched locations: {SearchedViewLocations}",
                    viewName,
                    viewEngineResult.SearchedLocations);
            }

            var view = viewEngineResult.EnsureSuccessful().View;

            if (telemetry.IsEnabled("Microsoft.AspNet.Mvc.ViewResultViewFound"))
            {
                telemetry.WriteTelemetry(
                    "Microsoft.AspNet.Mvc.ViewResultViewFound",
                    new { actionContext = context, result = this, viewName, view = view });
            }

            logger.LogVerbose("The view '{ViewName}' was found.", viewName);

            if (StatusCode != null)
            {
                context.HttpContext.Response.StatusCode = StatusCode.Value;
            }

            using (view as IDisposable)
            {
                await ViewExecutor.ExecuteAsync(
                    view,
                    context,
                    ViewData,
                    TempData,
                    options.Value.HtmlHelperOptions,
                    ContentType);
            }
        }