parent
60443101d5
commit
e95585dfbd
|
|
@ -8,13 +8,6 @@ namespace MvcSample.Web.RandomNameSpace
|
|||
{
|
||||
private User _user = new User() { Name = "User Name", Address = "Home Address" };
|
||||
|
||||
public Home2Controller(IActionResultHelper actionResultHelper)
|
||||
{
|
||||
Result = actionResultHelper;
|
||||
}
|
||||
|
||||
public IActionResultHelper Result { get; private set; }
|
||||
|
||||
public HttpContext Context
|
||||
{
|
||||
get
|
||||
|
|
@ -23,6 +16,7 @@ namespace MvcSample.Web.RandomNameSpace
|
|||
}
|
||||
}
|
||||
|
||||
// The property ActionContext gets injected by InitializeController from DefaultControllerFactory.
|
||||
public ActionContext ActionContext { get; set; }
|
||||
|
||||
public string Index()
|
||||
|
|
@ -40,7 +34,10 @@ namespace MvcSample.Web.RandomNameSpace
|
|||
|
||||
public ActionResult Hello()
|
||||
{
|
||||
return Result.Content("Hello World", null, null);
|
||||
return new ContentResult
|
||||
{
|
||||
Content = "Hello World",
|
||||
};
|
||||
}
|
||||
|
||||
public void Raw()
|
||||
|
|
@ -50,7 +47,7 @@ namespace MvcSample.Web.RandomNameSpace
|
|||
|
||||
public ActionResult UserJson()
|
||||
{
|
||||
var jsonResult = Result.Json(_user);
|
||||
var jsonResult = new JsonResult(_user);
|
||||
jsonResult.Indent = false;
|
||||
|
||||
return jsonResult;
|
||||
|
|
|
|||
|
|
@ -5,44 +5,47 @@ namespace MvcSample.Web
|
|||
{
|
||||
public class OverloadController
|
||||
{
|
||||
private readonly IActionResultHelper _result;
|
||||
|
||||
public OverloadController(IActionResultHelper result)
|
||||
{
|
||||
_result = result;
|
||||
}
|
||||
|
||||
// All results implement IActionResult so it can be safely returned.
|
||||
public IActionResult Get()
|
||||
{
|
||||
return _result.Content("Get()", null, null);
|
||||
return Content("Get()");
|
||||
}
|
||||
|
||||
public ActionResult Get(int id)
|
||||
{
|
||||
return _result.Content("Get(id)", null, null);
|
||||
return Content("Get(id)");
|
||||
}
|
||||
|
||||
public ActionResult Get(int id, string name)
|
||||
{
|
||||
return _result.Content("Get(id, name)", null, null);
|
||||
return Content("Get(id, name)");
|
||||
}
|
||||
|
||||
public ActionResult WithUser()
|
||||
{
|
||||
return _result.Content("WithUser()", null, null);
|
||||
return Content("WithUser()");
|
||||
}
|
||||
|
||||
// Called for all posts regardless of values provided
|
||||
[HttpPost]
|
||||
public ActionResult WithUser(User user)
|
||||
{
|
||||
return _result.Content("WithUser(User)", null, null);
|
||||
return Content("WithUser(User)");
|
||||
}
|
||||
|
||||
public ActionResult WithUser(int projectId, User user)
|
||||
{
|
||||
return _result.Content("WithUser(int, User)", null, null);
|
||||
return Content("WithUser(int, User)");
|
||||
}
|
||||
|
||||
private ContentResult Content(string content)
|
||||
{
|
||||
var result = new ContentResult
|
||||
{
|
||||
Content = content,
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,55 +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.Text;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public class ActionResultHelper : IActionResultHelper
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IViewEngine _viewEngine;
|
||||
|
||||
public ActionResultHelper(IServiceProvider serviceProvider, IViewEngine viewEngine)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_viewEngine = viewEngine;
|
||||
}
|
||||
|
||||
public ContentResult Content(string value, string contentType, Encoding contentEncoding)
|
||||
{
|
||||
var result = new ContentResult
|
||||
{
|
||||
Content = value,
|
||||
};
|
||||
|
||||
if (contentType != null)
|
||||
{
|
||||
result.Content = contentType;
|
||||
}
|
||||
|
||||
if (contentEncoding != null)
|
||||
{
|
||||
result.ContentEncoding = contentEncoding;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public JsonResult Json(object value)
|
||||
{
|
||||
return new JsonResult(value);
|
||||
}
|
||||
|
||||
public ViewResult View(string view, ViewDataDictionary viewData)
|
||||
{
|
||||
return new ViewResult(_serviceProvider, _viewEngine)
|
||||
{
|
||||
ViewName = view,
|
||||
ViewData = viewData
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -12,19 +12,13 @@ namespace Microsoft.AspNet.Mvc
|
|||
public class JsonResult : ActionResult
|
||||
{
|
||||
private const int BufferSize = 1024;
|
||||
private readonly object _returnValue;
|
||||
|
||||
private JsonSerializerSettings _jsonSerializerSettings;
|
||||
private Encoding _encoding = UTF8EncodingWithoutBOM.Encoding;
|
||||
|
||||
public JsonResult(object returnValue)
|
||||
public JsonResult([NotNull] object data)
|
||||
{
|
||||
if (returnValue == null)
|
||||
{
|
||||
throw new ArgumentNullException("returnValue");
|
||||
}
|
||||
|
||||
_returnValue = returnValue;
|
||||
Data = data;
|
||||
_jsonSerializerSettings = JsonOutputFormatter.CreateDefaultSettings();
|
||||
}
|
||||
|
||||
|
|
@ -62,6 +56,8 @@ namespace Microsoft.AspNet.Mvc
|
|||
}
|
||||
}
|
||||
|
||||
public object Data { get; private set; }
|
||||
|
||||
public override void ExecuteResult([NotNull] ActionContext context)
|
||||
{
|
||||
var response = context.HttpContext.Response;
|
||||
|
|
@ -75,7 +71,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
using (var writer = new StreamWriter(writeStream, Encoding, BufferSize, leaveOpen: true))
|
||||
{
|
||||
var formatter = new JsonOutputFormatter(SerializerSettings, Indent);
|
||||
formatter.WriteObject(writer, _returnValue);
|
||||
formatter.WriteObject(writer, Data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,11 +15,14 @@ namespace Microsoft.AspNet.Mvc
|
|||
public class Controller : IActionFilter, IAsyncActionFilter
|
||||
{
|
||||
private DynamicViewData _viewBag;
|
||||
private IServiceProvider _serviceProvider;
|
||||
private IViewEngine _viewEngine;
|
||||
|
||||
[NonAction]
|
||||
public void Initialize(IActionResultHelper actionResultHelper)
|
||||
public void Initialize(IServiceProvider serviceProvider, IViewEngine viewEngine)
|
||||
{
|
||||
Result = actionResultHelper;
|
||||
_serviceProvider = serviceProvider;
|
||||
_viewEngine = viewEngine;
|
||||
}
|
||||
|
||||
public HttpContext Context
|
||||
|
|
@ -40,8 +43,6 @@ namespace Microsoft.AspNet.Mvc
|
|||
|
||||
public ActionContext ActionContext { get; set; }
|
||||
|
||||
public IActionResultHelper Result { get; private set; }
|
||||
|
||||
public IUrlHelper Url { get; set; }
|
||||
|
||||
public IPrincipal User
|
||||
|
|
@ -100,7 +101,11 @@ namespace Microsoft.AspNet.Mvc
|
|||
ViewData.Model = model;
|
||||
}
|
||||
|
||||
return Result.View(view, ViewData);
|
||||
return new ViewResult(_serviceProvider, _viewEngine)
|
||||
{
|
||||
ViewName = view,
|
||||
ViewData = ViewData,
|
||||
};
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
|
|
@ -118,13 +123,28 @@ namespace Microsoft.AspNet.Mvc
|
|||
[NonAction]
|
||||
public ContentResult Content(string content, string contentType, Encoding contentEncoding)
|
||||
{
|
||||
return Result.Content(content, contentType, contentEncoding);
|
||||
var result = new ContentResult
|
||||
{
|
||||
Content = content,
|
||||
};
|
||||
|
||||
if (contentType != null)
|
||||
{
|
||||
result.ContentType = contentType;
|
||||
}
|
||||
|
||||
if (contentEncoding != null)
|
||||
{
|
||||
result.ContentEncoding = contentEncoding;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
public JsonResult Json(object value)
|
||||
{
|
||||
return Result.Json(value);
|
||||
return new JsonResult(value);
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
|
|
|
|||
|
|
@ -1,15 +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.Text;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public interface IActionResultHelper
|
||||
{
|
||||
ContentResult Content(string value, string contentType, Encoding contentEncoding);
|
||||
JsonResult Json(object value);
|
||||
ViewResult View(string view, ViewDataDictionary viewData);
|
||||
}
|
||||
}
|
||||
|
|
@ -32,7 +32,6 @@
|
|||
<Compile Include="ActionMethodSelectorAttribute.cs" />
|
||||
<Compile Include="ActionNameAttribute.cs" />
|
||||
<Compile Include="ActionResult.cs" />
|
||||
<Compile Include="ActionResultHelper.cs" />
|
||||
<Compile Include="ActionResults\ChallengeResult.cs" />
|
||||
<Compile Include="ActionResults\ContentResult.cs" />
|
||||
<Compile Include="ActionResults\EmptyResult.cs" />
|
||||
|
|
@ -130,7 +129,6 @@
|
|||
<Compile Include="IActionInvokerFactory.cs" />
|
||||
<Compile Include="IActionInvokerProvider.cs" />
|
||||
<Compile Include="IActionResult.cs" />
|
||||
<Compile Include="IActionResultHelper.cs" />
|
||||
<Compile Include="IActionSelector.cs" />
|
||||
<Compile Include="IControllerAssemblyProvider.cs" />
|
||||
<Compile Include="IControllerDescriptorFactory.cs" />
|
||||
|
|
|
|||
|
|
@ -29,7 +29,6 @@ namespace Microsoft.AspNet.Mvc
|
|||
yield return describe.Transient<IControllerDescriptorFactory, DefaultControllerDescriptorFactory>();
|
||||
yield return describe.Transient<IActionSelector, DefaultActionSelector>();
|
||||
yield return describe.Transient<IActionInvokerFactory, ActionInvokerFactory>();
|
||||
yield return describe.Transient<IActionResultHelper, ActionResultHelper>();
|
||||
yield return describe.Transient<IParameterDescriptorFactory, DefaultParameterDescriptorFactory>();
|
||||
yield return describe.Transient<IControllerAssemblyProvider, DefaultControllerAssemblyProvider>();
|
||||
yield return describe.Transient<IActionDiscoveryConventions, DefaultActionDiscoveryConventions>();
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.AspNet.Testing;
|
||||
|
|
@ -268,6 +269,147 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
Assert.True(method.IsDefined(typeof(NonActionAttribute)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Controller_View_WithoutParameter_SetsResultNullViewNameAndNullViewDataModel()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new Controller()
|
||||
{
|
||||
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
|
||||
};
|
||||
|
||||
// Act
|
||||
var actualViewResult = controller.View();
|
||||
|
||||
// Assert
|
||||
Assert.IsType<ViewResult>(actualViewResult);
|
||||
Assert.Null(actualViewResult.ViewName);
|
||||
Assert.Same(controller.ViewData, actualViewResult.ViewData);
|
||||
Assert.Null(actualViewResult.ViewData.Model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Controller_View_WithParameterViewName_SetsResultViewNameAndNullViewDataModel()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new Controller()
|
||||
{
|
||||
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
|
||||
};
|
||||
|
||||
// Act
|
||||
var actualViewResult = controller.View("CustomViewName");
|
||||
|
||||
// Assert
|
||||
Assert.IsType<ViewResult>(actualViewResult);
|
||||
Assert.Equal("CustomViewName", actualViewResult.ViewName);
|
||||
Assert.Same(controller.ViewData, actualViewResult.ViewData);
|
||||
Assert.Null(actualViewResult.ViewData.Model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Controller_View_WithParameterViewModel_SetsResultNullViewNameAndViewDataModel()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new Controller()
|
||||
{
|
||||
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
|
||||
};
|
||||
var model = new object();
|
||||
|
||||
// Act
|
||||
var actualViewResult = controller.View(model);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<ViewResult>(actualViewResult);
|
||||
Assert.Null(actualViewResult.ViewName);
|
||||
Assert.Same(controller.ViewData, actualViewResult.ViewData);
|
||||
Assert.Same(model, actualViewResult.ViewData.Model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Controller_View_WithParameterViewNameAndViewModel_SetsResultViewNameAndViewDataModel()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new Controller()
|
||||
{
|
||||
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
|
||||
};
|
||||
var model = new object();
|
||||
|
||||
// Act
|
||||
var actualViewResult = controller.View("CustomViewName", model);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<ViewResult>(actualViewResult);
|
||||
Assert.Equal("CustomViewName", actualViewResult.ViewName);
|
||||
Assert.Same(controller.ViewData, actualViewResult.ViewData);
|
||||
Assert.Same(model, actualViewResult.ViewData.Model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Controller_Content_WithParameterContentString_SetsResultContent()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new Controller();
|
||||
|
||||
// Act
|
||||
var actualContentResult = controller.Content("TestContent");
|
||||
|
||||
// Assert
|
||||
Assert.IsType<ContentResult>(actualContentResult);
|
||||
Assert.Equal("TestContent", actualContentResult.Content);
|
||||
Assert.Null(actualContentResult.ContentEncoding);
|
||||
Assert.Null(actualContentResult.ContentType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Controller_Content_WithParameterContentStringAndContentType_SetsResultContentAndContentType()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new Controller();
|
||||
|
||||
// Act
|
||||
var actualContentResult = controller.Content("TestContent", "text/plain");
|
||||
|
||||
// Assert
|
||||
Assert.IsType<ContentResult>(actualContentResult);
|
||||
Assert.Equal("TestContent", actualContentResult.Content);
|
||||
Assert.Null(actualContentResult.ContentEncoding);
|
||||
Assert.Equal("text/plain", actualContentResult.ContentType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Controller_Content_WithParameterContentAndTypeAndEncoding_SetsResultContentAndTypeAndEncoding()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new Controller();
|
||||
|
||||
// Act
|
||||
var actualContentResult = controller.Content("TestContent", "text/plain", Encoding.UTF8);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<ContentResult>(actualContentResult);
|
||||
Assert.Equal("TestContent", actualContentResult.Content);
|
||||
Assert.Same(Encoding.UTF8, actualContentResult.ContentEncoding);
|
||||
Assert.Equal("text/plain", actualContentResult.ContentType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Controller_Json_WithParameterValue_SetsResultData()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new Controller();
|
||||
var data = new object();
|
||||
|
||||
// Act
|
||||
var actualJsonResult = controller.Json(data);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<JsonResult>(actualJsonResult);
|
||||
Assert.Same(data, actualJsonResult.Data);
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> RedirectTestData
|
||||
{
|
||||
get
|
||||
|
|
|
|||
Loading…
Reference in New Issue