Added IActionResultHelper and other services.

- Support the Initialize pattern as an alternate ctor.
This commit is contained in:
David Fowler 2013-12-12 12:05:43 -08:00
parent b575d2c337
commit 485eb48114
10 changed files with 155 additions and 32 deletions

View File

@ -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();
}
}
}

View File

@ -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; }
}
}

View File

@ -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);
}
}

View File

@ -40,6 +40,7 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="ActionResultHelper.cs" />
<Compile Include="ContentResult.cs" />
<Compile Include="EmptyResult.cs" />
<Compile Include="IActionResult.cs" />
@ -51,6 +52,7 @@
<Compile Include="HttpStatusCodeResult.cs" />
<Compile Include="IActionInvoker.cs" />
<Compile Include="IActionInvokerFactory.cs" />
<Compile Include="IActionResultHelper.cs" />
<Compile Include="IControllerFactory.cs" />
<Compile Include="MvcHandler.cs" />
<Compile Include="MvcServices.cs" />

View File

@ -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<IActionInvokerFactory>();
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<string, object>))
{
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;

View File

@ -12,10 +12,11 @@ namespace Microsoft.AspNet.Mvc
return services;
}
private static void DoCallback(Action<Type, Type> callback)
public static void DoCallback(Action<Type, Type> callback)
{
callback(typeof(IControllerFactory), typeof(DefaultControllerFactory));
callback(typeof(IActionInvokerFactory), typeof(ControllerActionInvokerFactory));
callback(typeof(IActionResultHelper), typeof(ActionResultHelper));
}
}
}

View File

@ -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");
}
}
}

View File

@ -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");
}
}
}

View File

@ -54,17 +54,9 @@
</ItemGroup>
<ItemGroup>
<Content Include="packages.config" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</None>
<None Include="Web.Release.config">
<DependentUpon>Web.config</DependentUpon>
</None>
</ItemGroup>
<ItemGroup>
<Content Include="Web.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="Home2Controller.cs" />
<Compile Include="HomeController.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Startup.cs" />
@ -75,6 +67,15 @@
<Name>Microsoft.AspNet.Mvc</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="web.config" />
<None Include="web.Debug.config">
<DependentUpon>web.config</DependentUpon>
</None>
<None Include="web.Release.config">
<DependentUpon>web.config</DependentUpon>
</None>
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@ -1,11 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>