From 485eb4811467b1bb81a1b203e775c8be5e45e3a2 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Thu, 12 Dec 2013 12:05:43 -0800 Subject: [PATCH] Added IActionResultHelper and other services. - Support the Initialize pattern as an alternate ctor. --- Microsoft.AspNet.Mvc/ActionResultHelper.cs | 30 +++++++++++++ Microsoft.AspNet.Mvc/Controller.cs | 8 ++-- Microsoft.AspNet.Mvc/IActionResultHelper.cs | 10 +++++ .../Microsoft.AspNet.Mvc.csproj | 2 + Microsoft.AspNet.Mvc/MvcHandler.cs | 44 +++++++++++++++---- Microsoft.AspNet.Mvc/MvcServices.cs | 3 +- MvcSample/Home2Controller.cs | 40 +++++++++++++++++ MvcSample/HomeController.cs | 13 +++++- MvcSample/MvcSample.csproj | 19 ++++---- MvcSample/Web.config | 18 ++++---- 10 files changed, 155 insertions(+), 32 deletions(-) create mode 100644 Microsoft.AspNet.Mvc/ActionResultHelper.cs create mode 100644 Microsoft.AspNet.Mvc/IActionResultHelper.cs create mode 100644 MvcSample/Home2Controller.cs diff --git a/Microsoft.AspNet.Mvc/ActionResultHelper.cs b/Microsoft.AspNet.Mvc/ActionResultHelper.cs new file mode 100644 index 0000000000..38207cade0 --- /dev/null +++ b/Microsoft.AspNet.Mvc/ActionResultHelper.cs @@ -0,0 +1,30 @@ +using System; + +namespace Microsoft.AspNet.Mvc +{ + public class ActionResultHelper : IActionResultHelper + { + public IActionResult Content(string value) + { + return new ContentResult + { + Content = value + }; + } + + public IActionResult Content(string value, string contentType) + { + return new ContentResult + { + Content = value, + ContentType = contentType + }; + } + + public IActionResult Json(object value) + { + // TODO: Make this work at some point + throw new NotImplementedException(); + } + } +} diff --git a/Microsoft.AspNet.Mvc/Controller.cs b/Microsoft.AspNet.Mvc/Controller.cs index 72b94ecc99..d79a6b657c 100644 --- a/Microsoft.AspNet.Mvc/Controller.cs +++ b/Microsoft.AspNet.Mvc/Controller.cs @@ -4,12 +4,14 @@ using Microsoft.Owin; namespace Microsoft.AspNet.Mvc { public class Controller - { - public void Initialize(IOwinContext context) + { + public void Initialize(IActionResultHelper actionResultHelper) { - Context = context; + Result = actionResultHelper; } + public IActionResultHelper Result { get; private set; } + public IOwinContext Context { get; set; } } } diff --git a/Microsoft.AspNet.Mvc/IActionResultHelper.cs b/Microsoft.AspNet.Mvc/IActionResultHelper.cs new file mode 100644 index 0000000000..97940c81ef --- /dev/null +++ b/Microsoft.AspNet.Mvc/IActionResultHelper.cs @@ -0,0 +1,10 @@ + +namespace Microsoft.AspNet.Mvc +{ + public interface IActionResultHelper + { + IActionResult Content(string value); + IActionResult Content(string value, string contentType); + IActionResult Json(object value); + } +} diff --git a/Microsoft.AspNet.Mvc/Microsoft.AspNet.Mvc.csproj b/Microsoft.AspNet.Mvc/Microsoft.AspNet.Mvc.csproj index 3be7e25b31..60fdb5063f 100644 --- a/Microsoft.AspNet.Mvc/Microsoft.AspNet.Mvc.csproj +++ b/Microsoft.AspNet.Mvc/Microsoft.AspNet.Mvc.csproj @@ -40,6 +40,7 @@ + @@ -51,6 +52,7 @@ + diff --git a/Microsoft.AspNet.Mvc/MvcHandler.cs b/Microsoft.AspNet.Mvc/MvcHandler.cs index 77408ace6a..34c8fb87b7 100644 --- a/Microsoft.AspNet.Mvc/MvcHandler.cs +++ b/Microsoft.AspNet.Mvc/MvcHandler.cs @@ -1,4 +1,6 @@ using System; +using System.Linq; +using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNet.CoreServices; using Microsoft.Owin; @@ -35,22 +37,48 @@ namespace Microsoft.AspNet.Mvc throw new InvalidOperationException(String.Format("Couldn't find controller '{0}'.", controllerName)); } - var controllerBase = controller as Controller; - - if (controllerBase != null) - { - // TODO: Make this the controller context - controllerBase.Initialize(context); - } - var controllerContext = new ControllerContext(context, controller); + Initialize(controller, controllerContext); + IActionInvokerFactory invokerFactory = _serviceProvider.GetService(); var invoker = invokerFactory.CreateInvoker(controllerContext); return invoker.InvokeActionAsync(actionName); } + private void Initialize(object controller, ControllerContext controllerContext) + { + var controllerType = controller.GetType(); + + foreach (var prop in controllerType.GetProperties()) + { + if (prop.Name == "Context") + { + if (prop.PropertyType == typeof(IOwinContext)) + { + prop.SetValue(controller, controllerContext.HttpContext); + } + else if (prop.PropertyType == typeof(IDictionary)) + { + prop.SetValue(controller, controllerContext.HttpContext.Environment); + } + } + } + + var method = controllerType.GetMethod("Initialize"); + + if (method == null) + { + return; + } + + var args = method.GetParameters() + .Select(p => _serviceProvider.GetService(p.ParameterType)).ToArray(); + + method.Invoke(controller, args); + } + private static string GetPartOrDefault(string[] parts, int index, string defaultValue) { return index < parts.Length ? parts[index] : defaultValue; diff --git a/Microsoft.AspNet.Mvc/MvcServices.cs b/Microsoft.AspNet.Mvc/MvcServices.cs index c558b926ec..0672624be4 100644 --- a/Microsoft.AspNet.Mvc/MvcServices.cs +++ b/Microsoft.AspNet.Mvc/MvcServices.cs @@ -12,10 +12,11 @@ namespace Microsoft.AspNet.Mvc return services; } - private static void DoCallback(Action callback) + public static void DoCallback(Action callback) { callback(typeof(IControllerFactory), typeof(DefaultControllerFactory)); callback(typeof(IActionInvokerFactory), typeof(ControllerActionInvokerFactory)); + callback(typeof(IActionResultHelper), typeof(ActionResultHelper)); } } } diff --git a/MvcSample/Home2Controller.cs b/MvcSample/Home2Controller.cs new file mode 100644 index 0000000000..f9337caf58 --- /dev/null +++ b/MvcSample/Home2Controller.cs @@ -0,0 +1,40 @@ +using Microsoft.AspNet.Mvc; +using Microsoft.Owin; + +namespace MvcSample +{ + public class Home2Controller + { + public Home2Controller(IActionResultHelper actionResultHelper) + { + Result = actionResultHelper; + } + + public IActionResultHelper Result { get; private set; } + + public IOwinContext Context { get; private set; } + + public string Index() + { + return "Hello World"; + } + + public IActionResult Something() + { + return new ContentResult + { + Content = "Hello World From Content" + }; + } + + public IActionResult Hello() + { + return Result.Content("Hello World"); + } + + public void Raw() + { + Context.Response.Write("Hello World raw"); + } + } +} \ No newline at end of file diff --git a/MvcSample/HomeController.cs b/MvcSample/HomeController.cs index b992469504..068468cbe2 100644 --- a/MvcSample/HomeController.cs +++ b/MvcSample/HomeController.cs @@ -1,8 +1,9 @@ using Microsoft.AspNet.Mvc; +using Microsoft.Owin; namespace MvcSample { - public class HomeController + public class HomeController : Controller { public string Index() { @@ -16,5 +17,15 @@ namespace MvcSample Content = "Hello World From Content" }; } + + public IActionResult Hello() + { + return Result.Content("Hello World"); + } + + public void Raw() + { + Context.Response.Write("Hello World raw"); + } } } \ No newline at end of file diff --git a/MvcSample/MvcSample.csproj b/MvcSample/MvcSample.csproj index 1f173455d9..f022db652a 100644 --- a/MvcSample/MvcSample.csproj +++ b/MvcSample/MvcSample.csproj @@ -54,17 +54,9 @@ - - Web.config - - - Web.config - - - - + @@ -75,6 +67,15 @@ Microsoft.AspNet.Mvc + + + + web.config + + + web.config + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/MvcSample/Web.config b/MvcSample/Web.config index bfb640da20..787aa09c3f 100644 --- a/MvcSample/Web.config +++ b/MvcSample/Web.config @@ -1,11 +1,9 @@ - - + - - - - - \ No newline at end of file + + + + + + +