Reviving PartialViewResult and associated methods on Controller
Fixes #824
This commit is contained in:
parent
7666d5973f
commit
de77c92a0a
|
|
@ -0,0 +1,21 @@
|
||||||
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a <see cref="ViewResultBase"/> that renders a partial view.
|
||||||
|
/// </summary>
|
||||||
|
public class PartialViewResult : ViewResultBase
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override ViewEngineResult FindView([NotNull] IViewEngine viewEngine,
|
||||||
|
[NotNull] ActionContext context,
|
||||||
|
[NotNull] string viewName)
|
||||||
|
{
|
||||||
|
return viewEngine.FindPartialView(context, viewName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,147 +1,21 @@
|
||||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNet.Mvc.Core;
|
|
||||||
using Microsoft.AspNet.Mvc.Rendering;
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc
|
namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
public class ViewResult : ActionResult
|
/// <summary>
|
||||||
|
/// Represents a <see cref="ViewResultBase"/> that renders a view to the response.
|
||||||
|
/// </summary>
|
||||||
|
public class ViewResult : ViewResultBase
|
||||||
{
|
{
|
||||||
private const int BufferSize = 1024;
|
/// <inheritdoc />
|
||||||
|
protected override ViewEngineResult FindView([NotNull] IViewEngine viewEngine,
|
||||||
public string ViewName { get; set; }
|
[NotNull] ActionContext context,
|
||||||
|
[NotNull] string viewName)
|
||||||
public ViewDataDictionary ViewData { get; set; }
|
|
||||||
|
|
||||||
public IViewEngine ViewEngine { get; set; }
|
|
||||||
|
|
||||||
public override async Task ExecuteResultAsync([NotNull] ActionContext context)
|
|
||||||
{
|
{
|
||||||
var viewEngine = ViewEngine ?? context.HttpContext.RequestServices.GetService<ICompositeViewEngine>();
|
return viewEngine.FindView(context, viewName);
|
||||||
|
|
||||||
var viewName = ViewName ?? context.ActionDescriptor.Name;
|
|
||||||
var view = FindView(viewEngine, context, viewName);
|
|
||||||
|
|
||||||
using (view as IDisposable)
|
|
||||||
{
|
|
||||||
context.HttpContext.Response.ContentType = "text/html; charset=utf-8";
|
|
||||||
var wrappedStream = new StreamWrapper(context.HttpContext.Response.Body);
|
|
||||||
var encoding = Encodings.UTF8EncodingWithoutBOM;
|
|
||||||
using (var writer = new StreamWriter(wrappedStream, encoding, BufferSize, leaveOpen: true))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var viewContext = new ViewContext(context, view, ViewData, writer);
|
|
||||||
await view.RenderAsync(viewContext);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// Need to prevent writes/flushes on dispose because the StreamWriter will flush even if
|
|
||||||
// nothing got written. This leads to a response going out on the wire prematurely in case an
|
|
||||||
// exception is being thrown inside the try catch block.
|
|
||||||
wrappedStream.BlockWrites = true;
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IView FindView(IViewEngine viewEngine, ActionContext context, string viewName)
|
|
||||||
{
|
|
||||||
var result = viewEngine.FindView(context, viewName);
|
|
||||||
if (!result.Success)
|
|
||||||
{
|
|
||||||
var locations = string.Empty;
|
|
||||||
if (result.SearchedLocations != null)
|
|
||||||
{
|
|
||||||
locations = Environment.NewLine +
|
|
||||||
string.Join(Environment.NewLine, result.SearchedLocations);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new InvalidOperationException(Resources.FormatViewEngine_ViewNotFound(viewName, locations));
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.View;
|
|
||||||
}
|
|
||||||
|
|
||||||
private class StreamWrapper : Stream
|
|
||||||
{
|
|
||||||
private readonly Stream _wrappedStream;
|
|
||||||
|
|
||||||
public StreamWrapper([NotNull] Stream stream)
|
|
||||||
{
|
|
||||||
_wrappedStream = stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool BlockWrites { get; set; }
|
|
||||||
|
|
||||||
public override bool CanRead
|
|
||||||
{
|
|
||||||
get { return _wrappedStream.CanRead; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool CanSeek
|
|
||||||
{
|
|
||||||
get { return _wrappedStream.CanSeek; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool CanWrite
|
|
||||||
{
|
|
||||||
get { return _wrappedStream.CanWrite; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Flush()
|
|
||||||
{
|
|
||||||
if (!BlockWrites)
|
|
||||||
{
|
|
||||||
_wrappedStream.Flush();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public override long Length
|
|
||||||
{
|
|
||||||
get { return _wrappedStream.Length; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override long Position
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _wrappedStream.Position;
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_wrappedStream.Position = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int Read(byte[] buffer, int offset, int count)
|
|
||||||
{
|
|
||||||
return _wrappedStream.Read(buffer, offset, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override long Seek(long offset, SeekOrigin origin)
|
|
||||||
{
|
|
||||||
return Seek(offset, origin);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void SetLength(long value)
|
|
||||||
{
|
|
||||||
SetLength(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Write(byte[] buffer, int offset, int count)
|
|
||||||
{
|
|
||||||
if (!BlockWrites)
|
|
||||||
{
|
|
||||||
_wrappedStream.Write(buffer, offset, count);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,162 @@
|
||||||
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNet.Mvc.Core;
|
||||||
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
|
using Microsoft.Framework.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents the base type for an <see cref="ActionResult"/> that renders a view to the response.
|
||||||
|
/// </summary>
|
||||||
|
public abstract class ViewResultBase : ActionResult
|
||||||
|
{
|
||||||
|
private const int BufferSize = 1024;
|
||||||
|
|
||||||
|
public string ViewName { get; set; }
|
||||||
|
|
||||||
|
public ViewDataDictionary ViewData { get; set; }
|
||||||
|
|
||||||
|
public IViewEngine ViewEngine { get; set; }
|
||||||
|
|
||||||
|
public override async Task ExecuteResultAsync([NotNull] ActionContext context)
|
||||||
|
{
|
||||||
|
var viewEngine = ViewEngine ?? context.HttpContext.RequestServices.GetService<ICompositeViewEngine>();
|
||||||
|
|
||||||
|
var viewName = ViewName ?? context.ActionDescriptor.Name;
|
||||||
|
var view = FindViewInternal(viewEngine, context, viewName);
|
||||||
|
|
||||||
|
using (view as IDisposable)
|
||||||
|
{
|
||||||
|
context.HttpContext.Response.ContentType = "text/html; charset=utf-8";
|
||||||
|
var wrappedStream = new StreamWrapper(context.HttpContext.Response.Body);
|
||||||
|
var encoding = Encodings.UTF8EncodingWithoutBOM;
|
||||||
|
using (var writer = new StreamWriter(wrappedStream, encoding, BufferSize, leaveOpen: true))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var viewContext = new ViewContext(context, view, ViewData, writer);
|
||||||
|
await view.RenderAsync(viewContext);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Need to prevent writes/flushes on dispose because the StreamWriter will flush even if
|
||||||
|
// nothing got written. This leads to a response going out on the wire prematurely in case an
|
||||||
|
// exception is being thrown inside the try catch block.
|
||||||
|
wrappedStream.BlockWrites = true;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attempts to locate the view named <paramref name="viewName"/> using the specified
|
||||||
|
/// <paramref name="viewEngine"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="viewEngine">The <see cref="IViewEngine"/> used to locate the view.</param>
|
||||||
|
/// <param name="context">The <see cref="ActionContext"/> for the executing action.</param>
|
||||||
|
/// <param name="viewName">The view to find.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected abstract ViewEngineResult FindView(IViewEngine viewEngine,
|
||||||
|
ActionContext context,
|
||||||
|
string viewName);
|
||||||
|
|
||||||
|
private IView FindViewInternal(IViewEngine viewEngine, ActionContext context, string viewName)
|
||||||
|
{
|
||||||
|
var result = FindView(viewEngine, context, viewName);
|
||||||
|
if (!result.Success)
|
||||||
|
{
|
||||||
|
var locations = string.Empty;
|
||||||
|
if (result.SearchedLocations != null)
|
||||||
|
{
|
||||||
|
locations = Environment.NewLine +
|
||||||
|
string.Join(Environment.NewLine, result.SearchedLocations);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new InvalidOperationException(Resources.FormatViewEngine_ViewNotFound(viewName, locations));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.View;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class StreamWrapper : Stream
|
||||||
|
{
|
||||||
|
private readonly Stream _wrappedStream;
|
||||||
|
|
||||||
|
public StreamWrapper([NotNull] Stream stream)
|
||||||
|
{
|
||||||
|
_wrappedStream = stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool BlockWrites { get; set; }
|
||||||
|
|
||||||
|
public override bool CanRead
|
||||||
|
{
|
||||||
|
get { return _wrappedStream.CanRead; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool CanSeek
|
||||||
|
{
|
||||||
|
get { return _wrappedStream.CanSeek; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool CanWrite
|
||||||
|
{
|
||||||
|
get { return _wrappedStream.CanWrite; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Flush()
|
||||||
|
{
|
||||||
|
if (!BlockWrites)
|
||||||
|
{
|
||||||
|
_wrappedStream.Flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override long Length
|
||||||
|
{
|
||||||
|
get { return _wrappedStream.Length; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public override long Position
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _wrappedStream.Position;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_wrappedStream.Position = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int Read(byte[] buffer, int offset, int count)
|
||||||
|
{
|
||||||
|
return _wrappedStream.Read(buffer, offset, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override long Seek(long offset, SeekOrigin origin)
|
||||||
|
{
|
||||||
|
return Seek(offset, origin);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void SetLength(long value)
|
||||||
|
{
|
||||||
|
SetLength(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(byte[] buffer, int offset, int count)
|
||||||
|
{
|
||||||
|
if (!BlockWrites)
|
||||||
|
{
|
||||||
|
_wrappedStream.Write(buffer, offset, count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -187,6 +187,62 @@ namespace Microsoft.AspNet.Mvc
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a <see cref="PartialViewResult"/> object that renders a partial view to the response.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The created <see cref="PartialViewResult"/> object for the response.</returns>
|
||||||
|
[NonAction]
|
||||||
|
public virtual PartialViewResult PartialView()
|
||||||
|
{
|
||||||
|
return PartialView(viewName: null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a <see cref="PartialViewResult"/> object by specifying a <paramref name="viewName"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="viewName">The name of the view that is rendered to the response.</param>
|
||||||
|
/// <returns>The created <see cref="PartialViewResult"/> object for the response.</returns>
|
||||||
|
[NonAction]
|
||||||
|
public virtual PartialViewResult PartialView(string viewName)
|
||||||
|
{
|
||||||
|
return PartialView(viewName, model: null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a <see cref="PartialViewResult"/> object by specifying a <paramref name="model"/>
|
||||||
|
/// to be rendered by the partial view.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="model">The model that is rendered by the partial view.</param>
|
||||||
|
/// <returns>The created <see cref="PartialViewResult"/> object for the response.</returns>
|
||||||
|
[NonAction]
|
||||||
|
public virtual PartialViewResult PartialView(object model)
|
||||||
|
{
|
||||||
|
return PartialView(viewName: null, model: model);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a <see cref="PartialViewResult"/> object by specifying a <paramref name="viewName"/>
|
||||||
|
/// and the <paramref name="model"/> to be rendered by the partial view.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="viewName">The name of the partial view that is rendered to the response.</param>
|
||||||
|
/// <param name="model">The model that is rendered by the partial view.</param>
|
||||||
|
/// <returns>The created <see cref="PartialViewResult"/> object for the response.</returns>
|
||||||
|
[NonAction]
|
||||||
|
public virtual PartialViewResult PartialView(string viewName, object model)
|
||||||
|
{
|
||||||
|
// Do not override ViewData.Model unless passed a non-null value.
|
||||||
|
if (model != null)
|
||||||
|
{
|
||||||
|
ViewData.Model = model;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new PartialViewResult()
|
||||||
|
{
|
||||||
|
ViewName = viewName,
|
||||||
|
ViewData = ViewData,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a <see cref="ContentResult"/> object by specifying a <paramref name="content"/> string.
|
/// Creates a <see cref="ContentResult"/> object by specifying a <paramref name="content"/> string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
|
using Microsoft.AspNet.PipelineCore;
|
||||||
|
using Microsoft.AspNet.Routing;
|
||||||
|
using Moq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc.Core
|
||||||
|
{
|
||||||
|
public class PartialViewResultTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task PartialViewResult_UsesFindViewOnSpecifiedViewEngineToLocateViews()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var viewName = "mypartialview";
|
||||||
|
var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
|
||||||
|
var viewEngine = new Mock<IViewEngine>();
|
||||||
|
var view = Mock.Of<IView>();
|
||||||
|
|
||||||
|
viewEngine.Setup(e => e.FindPartialView(context, viewName))
|
||||||
|
.Returns(ViewEngineResult.Found(viewName, view))
|
||||||
|
.Verifiable();
|
||||||
|
|
||||||
|
var viewResult = new PartialViewResult
|
||||||
|
{
|
||||||
|
ViewEngine = viewEngine.Object,
|
||||||
|
ViewName = viewName
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await viewResult.ExecuteResultAsync(context);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
viewEngine.Verify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,243 @@
|
||||||
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
|
using Microsoft.AspNet.PipelineCore;
|
||||||
|
using Microsoft.AspNet.Routing;
|
||||||
|
using Moq;
|
||||||
|
using Moq.Protected;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc.Core
|
||||||
|
{
|
||||||
|
public class ViewResultBaseTest
|
||||||
|
{
|
||||||
|
// The buffer size of the StreamWriter used in ViewResult.
|
||||||
|
private const int ViewResultStreamWriterBufferSize = 1024;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ExecuteResultAsync_ReturnsError_IfViewCouldNotBeFound()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expected = string.Join(Environment.NewLine,
|
||||||
|
"The view 'MyView' was not found. The following locations were searched:",
|
||||||
|
"Location1",
|
||||||
|
"Location2.");
|
||||||
|
var actionContext = new ActionContext(new DefaultHttpContext(),
|
||||||
|
new RouteData(),
|
||||||
|
new ActionDescriptor());
|
||||||
|
var viewEngine = Mock.Of<IViewEngine>();
|
||||||
|
|
||||||
|
var mockResult = new Mock<ViewResultBase> { CallBase = true };
|
||||||
|
mockResult.Protected()
|
||||||
|
.Setup<ViewEngineResult>("FindView", viewEngine, actionContext, ItExpr.IsAny<string>())
|
||||||
|
.Returns(ViewEngineResult.NotFound("MyView", new[] { "Location1", "Location2" }))
|
||||||
|
.Verifiable();
|
||||||
|
|
||||||
|
var viewResult = mockResult.Object;
|
||||||
|
viewResult.ViewEngine = viewEngine;
|
||||||
|
viewResult.ViewName = "MyView";
|
||||||
|
|
||||||
|
// Act and Assert
|
||||||
|
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
|
||||||
|
() => viewResult.ExecuteResultAsync(actionContext));
|
||||||
|
Assert.Equal(expected, ex.Message);
|
||||||
|
mockResult.Verify();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ExecuteResultAsync_UsesActionDescriptorName_IfViewNameIsNullOrEmpty()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var viewName = "some-view-name";
|
||||||
|
var actionContext = new ActionContext(new DefaultHttpContext(),
|
||||||
|
new RouteData(),
|
||||||
|
new ActionDescriptor { Name = viewName });
|
||||||
|
var viewEngine = Mock.Of<IViewEngine>();
|
||||||
|
|
||||||
|
var mockResult = new Mock<ViewResultBase> { CallBase = true };
|
||||||
|
mockResult.Protected()
|
||||||
|
.Setup<ViewEngineResult>("FindView", viewEngine, actionContext, viewName)
|
||||||
|
.Returns(ViewEngineResult.Found(viewName, Mock.Of<IView>()))
|
||||||
|
.Verifiable();
|
||||||
|
|
||||||
|
var viewResult = mockResult.Object;
|
||||||
|
viewResult.ViewEngine = viewEngine;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await viewResult.ExecuteResultAsync(actionContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
mockResult.Verify();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ExecuteResultAsync_WritesOutputWithoutBOM()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expected = new byte[] { 97, 98, 99, 100 };
|
||||||
|
|
||||||
|
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 viewEngine = Mock.Of<ICompositeViewEngine>();
|
||||||
|
var routeDictionary = new Dictionary<string, object>();
|
||||||
|
var serviceProvider = new Mock<IServiceProvider>();
|
||||||
|
serviceProvider.Setup(sp => sp.GetService(typeof(ICompositeViewEngine)))
|
||||||
|
.Returns(viewEngine);
|
||||||
|
|
||||||
|
var context = new DefaultHttpContext
|
||||||
|
{
|
||||||
|
RequestServices = serviceProvider.Object,
|
||||||
|
};
|
||||||
|
var memoryStream = new MemoryStream();
|
||||||
|
context.Response.Body = memoryStream;
|
||||||
|
|
||||||
|
var actionContext = new ActionContext(context,
|
||||||
|
new RouteData { Values = routeDictionary },
|
||||||
|
new ActionDescriptor());
|
||||||
|
|
||||||
|
var viewResult = new Mock<ViewResultBase> { CallBase = true };
|
||||||
|
|
||||||
|
|
||||||
|
var result = new Mock<ViewResultBase> { CallBase = true };
|
||||||
|
result.Protected()
|
||||||
|
.Setup<ViewEngineResult>("FindView", viewEngine, actionContext, ItExpr.IsAny<string>())
|
||||||
|
.Returns(ViewEngineResult.Found("MyView", view.Object));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await result.Object.ExecuteResultAsync(actionContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(expected, memoryStream.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ExecuteResultAsync_UsesProvidedViewEngine()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expected = new byte[] { 97, 98, 99, 100 };
|
||||||
|
|
||||||
|
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 routeDictionary = new Dictionary<string, object>();
|
||||||
|
|
||||||
|
var goodViewEngine = Mock.Of<IViewEngine>();
|
||||||
|
var badViewEngine = new Mock<ICompositeViewEngine>(MockBehavior.Strict);
|
||||||
|
|
||||||
|
var serviceProvider = new Mock<IServiceProvider>();
|
||||||
|
serviceProvider.Setup(sp => sp.GetService(typeof(ICompositeViewEngine)))
|
||||||
|
.Returns(badViewEngine.Object);
|
||||||
|
|
||||||
|
var context = new DefaultHttpContext
|
||||||
|
{
|
||||||
|
RequestServices = serviceProvider.Object,
|
||||||
|
};
|
||||||
|
var memoryStream = new MemoryStream();
|
||||||
|
context.Response.Body = memoryStream;
|
||||||
|
|
||||||
|
var actionContext = new ActionContext(context,
|
||||||
|
new RouteData { Values = routeDictionary },
|
||||||
|
new ActionDescriptor());
|
||||||
|
|
||||||
|
var result = new Mock<ViewResultBase> { CallBase = true };
|
||||||
|
result.Protected()
|
||||||
|
.Setup<ViewEngineResult>("FindView", goodViewEngine, actionContext, ItExpr.IsAny<string>())
|
||||||
|
.Returns(ViewEngineResult.Found("MyView", view.Object))
|
||||||
|
.Verifiable();
|
||||||
|
|
||||||
|
result.Object.ViewEngine = goodViewEngine;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await result.Object.ExecuteResultAsync(actionContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(expected, memoryStream.ToArray());
|
||||||
|
result.Verify();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> ExecuteResultAsync_DoesNotWriteToResponse_OnceExceptionIsThrownData
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new object[] { 30, 0 };
|
||||||
|
|
||||||
|
if (PlatformHelper.IsMono)
|
||||||
|
{
|
||||||
|
// The StreamWriter in Mono buffers 2x the buffer size before flushing.
|
||||||
|
yield return new object[] { ViewResultStreamWriterBufferSize * 2 + 30, ViewResultStreamWriterBufferSize };
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
yield return new object[] { ViewResultStreamWriterBufferSize + 30, ViewResultStreamWriterBufferSize };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The StreamWriter used by ViewResult an internal buffer and consequently anything written to this buffer
|
||||||
|
// prior to it filling up will not be written to the underlying stream once an exception is thrown.
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(ExecuteResultAsync_DoesNotWriteToResponse_OnceExceptionIsThrownData))]
|
||||||
|
public async Task ExecuteResultAsync_DoesNotWriteToResponse_OnceExceptionIsThrown(int writtenLength, int expectedLength)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var longString = new string('a', writtenLength);
|
||||||
|
|
||||||
|
var routeDictionary = new Dictionary<string, object>();
|
||||||
|
|
||||||
|
var view = new Mock<IView>();
|
||||||
|
view.Setup(v => v.RenderAsync(It.IsAny<ViewContext>()))
|
||||||
|
.Callback((ViewContext v) =>
|
||||||
|
{
|
||||||
|
view.ToString();
|
||||||
|
v.Writer.Write(longString);
|
||||||
|
throw new Exception();
|
||||||
|
});
|
||||||
|
|
||||||
|
var viewEngine = Mock.Of<ICompositeViewEngine>();
|
||||||
|
var serviceProvider = new Mock<IServiceProvider>();
|
||||||
|
serviceProvider.Setup(sp => sp.GetService(typeof(ICompositeViewEngine)))
|
||||||
|
.Returns(viewEngine);
|
||||||
|
|
||||||
|
var context = new DefaultHttpContext
|
||||||
|
{
|
||||||
|
RequestServices = serviceProvider.Object,
|
||||||
|
};
|
||||||
|
var memoryStream = new MemoryStream();
|
||||||
|
context.Response.Body = memoryStream;
|
||||||
|
|
||||||
|
var actionContext = new ActionContext(context,
|
||||||
|
new RouteData { Values = routeDictionary },
|
||||||
|
new ActionDescriptor());
|
||||||
|
|
||||||
|
var result = new Mock<ViewResultBase> { CallBase = true };
|
||||||
|
result.Protected()
|
||||||
|
.Setup<ViewEngineResult>("FindView", viewEngine, actionContext, ItExpr.IsAny<string>())
|
||||||
|
.Returns(ViewEngineResult.Found("MyView", view.Object))
|
||||||
|
.Verifiable();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await Record.ExceptionAsync(() => result.Object.ExecuteResultAsync(actionContext));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(expectedLength, memoryStream.Length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
|
using Microsoft.AspNet.PipelineCore;
|
||||||
|
using Microsoft.AspNet.Routing;
|
||||||
|
using Moq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc.Core
|
||||||
|
{
|
||||||
|
public class ViewResultTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task ViewResult_UsesFindViewOnSpecifiedViewEngineToLocateViews()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var viewName = "myview";
|
||||||
|
var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
|
||||||
|
var viewEngine = new Mock<IViewEngine>();
|
||||||
|
var view = Mock.Of<IView>();
|
||||||
|
|
||||||
|
viewEngine.Setup(e => e.FindView(context, "myview"))
|
||||||
|
.Returns(ViewEngineResult.Found("myview", view))
|
||||||
|
.Verifiable();
|
||||||
|
|
||||||
|
var viewResult = new ViewResult
|
||||||
|
{
|
||||||
|
ViewName = viewName,
|
||||||
|
ViewEngine = viewEngine.Object
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await viewResult.ExecuteResultAsync(context);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
viewEngine.Verify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,196 +0,0 @@
|
||||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNet.Http;
|
|
||||||
using Microsoft.AspNet.Mvc.Rendering;
|
|
||||||
using Microsoft.AspNet.Routing;
|
|
||||||
using Moq;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.Core.Test
|
|
||||||
{
|
|
||||||
public class ViewResultTest
|
|
||||||
{
|
|
||||||
// The buffer size of the StreamWriter used in ViewResult.
|
|
||||||
private const int ViewResultStreamWriterBufferSize = 1024;
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task ExecuteResultAsync_WritesOutputWithoutBOM()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var expected = new byte[] { 97, 98, 99, 100 };
|
|
||||||
|
|
||||||
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 routeDictionary = new Dictionary<string, object>();
|
|
||||||
|
|
||||||
var viewEngine = new Mock<ICompositeViewEngine>();
|
|
||||||
|
|
||||||
var serviceProvider = new Mock<IServiceProvider>();
|
|
||||||
serviceProvider.Setup(sp => sp.GetService(typeof(ICompositeViewEngine)))
|
|
||||||
.Returns(viewEngine.Object);
|
|
||||||
|
|
||||||
var memoryStream = new MemoryStream();
|
|
||||||
var response = new Mock<HttpResponse>();
|
|
||||||
response.SetupGet(r => r.Body)
|
|
||||||
.Returns(memoryStream);
|
|
||||||
|
|
||||||
var context = new Mock<HttpContext>();
|
|
||||||
context.SetupGet(c => c.Response)
|
|
||||||
.Returns(response.Object);
|
|
||||||
context.SetupGet(c => c.RequestServices)
|
|
||||||
.Returns(serviceProvider.Object);
|
|
||||||
|
|
||||||
var actionContext = new ActionContext(context.Object,
|
|
||||||
new RouteData() { Values = routeDictionary },
|
|
||||||
new ActionDescriptor());
|
|
||||||
|
|
||||||
viewEngine.Setup(v => v.FindView(actionContext, It.IsAny<string>()))
|
|
||||||
.Returns(ViewEngineResult.Found("MyView", view.Object));
|
|
||||||
|
|
||||||
|
|
||||||
var viewResult = new ViewResult();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await viewResult.ExecuteResultAsync(actionContext);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.Equal(expected, memoryStream.ToArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task ExecuteResultAsync_UsesProvidedViewEngine()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var expected = new byte[] { 97, 98, 99, 100 };
|
|
||||||
|
|
||||||
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 routeDictionary = new Dictionary<string, object>();
|
|
||||||
|
|
||||||
var goodViewEngine = new Mock<IViewEngine>();
|
|
||||||
|
|
||||||
var badViewEngine = new Mock<ICompositeViewEngine>(MockBehavior.Strict);
|
|
||||||
|
|
||||||
var serviceProvider = new Mock<IServiceProvider>();
|
|
||||||
serviceProvider.Setup(sp => sp.GetService(typeof(ICompositeViewEngine)))
|
|
||||||
.Returns(badViewEngine.Object);
|
|
||||||
|
|
||||||
var memoryStream = new MemoryStream();
|
|
||||||
var response = new Mock<HttpResponse>();
|
|
||||||
response.SetupGet(r => r.Body)
|
|
||||||
.Returns(memoryStream);
|
|
||||||
|
|
||||||
var context = new Mock<HttpContext>();
|
|
||||||
context.SetupGet(c => c.Response)
|
|
||||||
.Returns(response.Object);
|
|
||||||
context.SetupGet(c => c.RequestServices)
|
|
||||||
.Returns(serviceProvider.Object);
|
|
||||||
|
|
||||||
var actionContext = new ActionContext(context.Object,
|
|
||||||
new RouteData() { Values = routeDictionary },
|
|
||||||
new ActionDescriptor());
|
|
||||||
|
|
||||||
goodViewEngine.Setup(v => v.FindView(actionContext, It.IsAny<string>()))
|
|
||||||
.Returns(ViewEngineResult.Found("MyView", view.Object));
|
|
||||||
|
|
||||||
|
|
||||||
var viewResult = new ViewResult()
|
|
||||||
{
|
|
||||||
ViewEngine = goodViewEngine.Object,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await viewResult.ExecuteResultAsync(actionContext);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.Equal(expected, memoryStream.ToArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IEnumerable<object[]> ExecuteResultAsync_DoesNotWriteToResponse_OnceExceptionIsThrownData
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
yield return new object[] { 30, 0 };
|
|
||||||
|
|
||||||
if (PlatformHelper.IsMono)
|
|
||||||
{
|
|
||||||
// The StreamWriter in Mono buffers 2x the buffer size before flushing.
|
|
||||||
yield return new object[] { ViewResultStreamWriterBufferSize * 2 + 30, ViewResultStreamWriterBufferSize };
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
yield return new object[] { ViewResultStreamWriterBufferSize + 30, ViewResultStreamWriterBufferSize };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The StreamWriter used by ViewResult an internal buffer and consequently anything written to this buffer
|
|
||||||
// prior to it filling up will not be written to the underlying stream once an exception is thrown.
|
|
||||||
[Theory]
|
|
||||||
[MemberData(nameof(ExecuteResultAsync_DoesNotWriteToResponse_OnceExceptionIsThrownData))]
|
|
||||||
public async Task ExecuteResultAsync_DoesNotWriteToResponse_OnceExceptionIsThrown(int writtenLength, int expectedLength)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var longString = new string('a', writtenLength);
|
|
||||||
|
|
||||||
var routeDictionary = new Dictionary<string, object>();
|
|
||||||
|
|
||||||
var view = new Mock<IView>();
|
|
||||||
view.Setup(v => v.RenderAsync(It.IsAny<ViewContext>()))
|
|
||||||
.Callback((ViewContext v) =>
|
|
||||||
{
|
|
||||||
view.ToString();
|
|
||||||
v.Writer.Write(longString);
|
|
||||||
throw new Exception();
|
|
||||||
});
|
|
||||||
|
|
||||||
var viewEngine = new Mock<ICompositeViewEngine>();
|
|
||||||
viewEngine.Setup(v => v.FindView(It.IsAny<ActionContext>(), It.IsAny<string>()))
|
|
||||||
.Returns(ViewEngineResult.Found("MyView", view.Object));
|
|
||||||
|
|
||||||
var serviceProvider = new Mock<IServiceProvider>();
|
|
||||||
serviceProvider.Setup(sp => sp.GetService(typeof(ICompositeViewEngine)))
|
|
||||||
.Returns(viewEngine.Object);
|
|
||||||
|
|
||||||
var memoryStream = new MemoryStream();
|
|
||||||
var response = new Mock<HttpResponse>();
|
|
||||||
response.SetupGet(r => r.Body)
|
|
||||||
.Returns(memoryStream);
|
|
||||||
var context = new Mock<HttpContext>();
|
|
||||||
context.SetupGet(c => c.Response)
|
|
||||||
.Returns(response.Object);
|
|
||||||
context.SetupGet(c => c.RequestServices).Returns(serviceProvider.Object);
|
|
||||||
|
|
||||||
var actionContext = new ActionContext(context.Object,
|
|
||||||
new RouteData() { Values = routeDictionary },
|
|
||||||
new ActionDescriptor());
|
|
||||||
|
|
||||||
var viewResult = new ViewResult();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await Record.ExceptionAsync(() => viewResult.ExecuteResultAsync(actionContext));
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.Equal(expectedLength, memoryStream.Length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -49,6 +49,12 @@ ViewWithNestedLayout-Content
|
||||||
</nested-layout>
|
</nested-layout>
|
||||||
</layout>"
|
</layout>"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
yield return new[]
|
||||||
|
{
|
||||||
|
"ViewWithDataFromController",
|
||||||
|
"<h1>hello from controller</h1>"
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -141,5 +147,54 @@ component-content";
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(expected, body.Trim());
|
Assert.Equal(expected, body.Trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> PartialRazorViews_DoNotRenderLayoutData
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new[]
|
||||||
|
{
|
||||||
|
"ViewWithoutLayout", @"ViewWithoutLayout-Content"
|
||||||
|
};
|
||||||
|
yield return new[]
|
||||||
|
{
|
||||||
|
"PartialViewWithNamePassedIn", @"ViewWithLayout-Content"
|
||||||
|
};
|
||||||
|
yield return new[]
|
||||||
|
{
|
||||||
|
"ViewWithFullPath", "ViewWithFullPath-content"
|
||||||
|
};
|
||||||
|
yield return new[]
|
||||||
|
{
|
||||||
|
"ViewWithNestedLayout", "ViewWithNestedLayout-Content"
|
||||||
|
};
|
||||||
|
yield return new[]
|
||||||
|
{
|
||||||
|
"PartialWithDataFromController", "<h1>hello from controller</h1>"
|
||||||
|
};
|
||||||
|
yield return new[]
|
||||||
|
{
|
||||||
|
"PartialWithModel", string.Join(Environment.NewLine,
|
||||||
|
"my name is judge",
|
||||||
|
"<partial>98052",
|
||||||
|
"</partial>")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(PartialRazorViews_DoNotRenderLayoutData))]
|
||||||
|
public async Task PartialRazorViews_DoNotRenderLayout(string actionName, string expected)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var server = TestServer.Create(_provider, _app);
|
||||||
|
var client = server.CreateClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var body = await client.GetStringAsync("http://localhost/PartialViewEngine/" + actionName);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(expected, body.Trim());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
|
|
||||||
|
namespace RazorWebSite.Controllers
|
||||||
|
{
|
||||||
|
public class PartialViewEngineController : Controller
|
||||||
|
{
|
||||||
|
public IActionResult ViewWithoutLayout()
|
||||||
|
{
|
||||||
|
return PartialView();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult ViewWithFullPath()
|
||||||
|
{
|
||||||
|
return PartialView(@"/Views/ViewEngine/ViewWithFullPath.cshtml");
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult PartialViewWithNamePassedIn()
|
||||||
|
{
|
||||||
|
return PartialView("ViewWithLayout");
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult ViewWithNestedLayout()
|
||||||
|
{
|
||||||
|
return PartialView();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult PartialWithDataFromController()
|
||||||
|
{
|
||||||
|
ViewData["data-from-controller"] = "hello from controller";
|
||||||
|
return PartialView("ViewWithDataFromController");
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult PartialWithModel()
|
||||||
|
{
|
||||||
|
var model = new Person
|
||||||
|
{
|
||||||
|
Name = "my name is judge",
|
||||||
|
Address = new Address { ZipCode = "98052"}
|
||||||
|
};
|
||||||
|
return PartialView(model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -42,5 +42,11 @@ namespace RazorWebSite.Controllers
|
||||||
ViewData["Title"] = "Controller title";
|
ViewData["Title"] = "Controller title";
|
||||||
return View("ViewWithTitle");
|
return View("ViewWithTitle");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IActionResult ViewWithDataFromController()
|
||||||
|
{
|
||||||
|
ViewData["data-from-controller"] = "hello from controller";
|
||||||
|
return View("ViewWithDataFromController");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
@model Person
|
||||||
|
@Model.Name
|
||||||
|
<partial>@Html.Partial("_Partial", Model.Address)</partial>
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
<h1>@ViewData["data-from-controller"]</h1>
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
@{
|
||||||
|
Layout = "/Views/Shared/_Layout.cshtml";
|
||||||
|
}
|
||||||
|
ViewWithFullPath-content
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
@{
|
||||||
|
Layout = "/Views/Shared/_Layout.cshtml";
|
||||||
|
}
|
||||||
|
ViewWithLayout-Content
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
@{
|
||||||
|
Layout = "/Views/ViewEngine/_NestedLayout.cshtml";
|
||||||
|
}
|
||||||
|
ViewWithNestedLayout-Content
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
ViewWithoutLayout-Content
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
<h1>@ViewData["data-from-controller"]</h1>
|
||||||
Loading…
Reference in New Issue