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

Commit

Permalink
* Allow null ViewData and TempData
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanbrandenburg committed Oct 12, 2015
1 parent 35d176f commit 9067ce7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
15 changes: 0 additions & 15 deletions src/Microsoft.AspNet.Mvc.ViewFeatures/Rendering/ViewContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ public ViewContext(
throw new ArgumentNullException(nameof(view));
}

if (viewData == null)
{
throw new ArgumentNullException(nameof(viewData));
}

if (tempData == null)
{
throw new ArgumentNullException(nameof(tempData));
}

if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
Expand Down Expand Up @@ -114,11 +104,6 @@ public ViewContext(
throw new ArgumentNullException(nameof(view));
}

if (viewData == null)
{
throw new ArgumentNullException(nameof(viewData));
}

if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
Expand Down
10 changes: 0 additions & 10 deletions src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/ViewExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@ public virtual async Task ExecuteAsync(
throw new ArgumentNullException(nameof(view));
}

if (viewData == null)
{
throw new ArgumentNullException(nameof(viewData));
}

if (tempData == null)
{
throw new ArgumentNullException(nameof(tempData));
}

var response = actionContext.HttpContext.Response;

if (contentType != null && contentType.Encoding == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. 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.Text;
using System.Threading;
Expand Down
48 changes: 48 additions & 0 deletions test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewResultTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,54 @@ public async Task ExecuteResultAsync_FindsAndExecutesView()
viewEngine.Verify();
}

[Fact]
public async Task ExecuteResultAsync_ViewResultAllowNullViewDataAndTempData()
{
var context = GetActionContext();
var controller = new TestController(context);

await controller.Index().ExecuteResultAsync(context);
Assert.Null(controller.ViewContext.ViewData);
Assert.Null(controller.ViewContext.TempData);
}

private class TestController
{
public ViewContext ViewContext { get; set; }
private IViewEngine _engine;
public TestController(ActionContext context)
{
var view = new Mock<IView>(MockBehavior.Strict);
view
.Setup(v => v.RenderAsync(It.IsAny<ViewContext>())).Callback((ViewContext viewContext) =>
{
ViewContext = viewContext;
})
.Returns(Task.FromResult(0))
.Verifiable();

view
.As<IDisposable>()
.Setup(v => v.Dispose())
.Verifiable();
var viewEngine = new Mock<IViewEngine>(MockBehavior.Strict);
viewEngine
.Setup(e => e.FindView(context, "myview"))
.Returns(ViewEngineResult.Found("myview", view.Object))
.Verifiable();

_engine = viewEngine.Object;
}

public IActionResult Index()
{
return new ViewResult {
ViewEngine = _engine,
ViewName = "myview"
};
}
}

private ActionContext GetActionContext()
{
return new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor());
Expand Down

0 comments on commit 9067ce7

Please sign in to comment.