Added more stuff:

- Added IActionResult and family.
This commit is contained in:
David Fowler 2013-12-12 10:52:20 -08:00
parent 3dfd79a26d
commit fe173fca1e
10 changed files with 133 additions and 18 deletions

View File

@ -0,0 +1,41 @@
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin;
namespace Microsoft.AspNet.Mvc
{
public class ContentResult : IActionResult
{
public string Content { get; set; }
public Encoding ContentEncoding { get; set; }
public string ContentType { get; set; }
public async Task ExecuteResultAsync(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
IOwinResponse response = context.HttpContext.Response;
if (!String.IsNullOrEmpty(ContentType))
{
response.ContentType = ContentType;
}
//if (ContentEncoding != null)
//{
// response.ContentEncoding = ContentEncoding;
//}
if (Content != null)
{
await response.WriteAsync(Content);
}
}
}
}

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Globalization;
using System.Linq; using System.Reflection;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Microsoft.AspNet.Mvc namespace Microsoft.AspNet.Mvc
@ -17,16 +16,38 @@ namespace Microsoft.AspNet.Mvc
public Task InvokeActionAsync(string actionName) public Task InvokeActionAsync(string actionName)
{ {
var method = _context.Controller.GetType().GetMethod(actionName); var method = _context.Controller.GetType().GetMethod(actionName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (method == null) if (method == null)
{ {
throw new InvalidOperationException(String.Format("Could not find action method '{0}'", actionName)); throw new InvalidOperationException(String.Format("Could not find action method '{0}'", actionName));
} }
method.Invoke(_context.Controller, null); object actionReturnValue = method.Invoke(_context.Controller, null); ;
return Task.FromResult(0); IActionResult actionResult = CreateResult(actionReturnValue);
return actionResult.ExecuteResultAsync(_context);
}
private IActionResult CreateResult(object actionReturnValue)
{
IActionResult actionResult = actionReturnValue as IActionResult;
if (actionResult != null)
{
return actionResult;
}
if (actionReturnValue != null)
{
return new ContentResult
{
Content = Convert.ToString(actionReturnValue, CultureInfo.InvariantCulture)
};
}
return new EmptyResult();
} }
} }
} }

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Reflection;
using Microsoft.AspNet.CoreServices; using Microsoft.AspNet.CoreServices;
using Microsoft.Owin; using Microsoft.Owin;
@ -17,6 +16,11 @@ namespace Microsoft.AspNet.Mvc
public object CreateController(IOwinContext context, string controllerName) public object CreateController(IOwinContext context, string controllerName)
{ {
if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
{
controllerName += "Controller";
}
foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
{ {
var type = a.GetType(controllerName) ?? var type = a.GetType(controllerName) ??

View File

@ -0,0 +1,18 @@
using System.Threading.Tasks;
namespace Microsoft.AspNet.Mvc
{
public class EmptyResult : IActionResult
{
private static readonly EmptyResult _singleton = new EmptyResult();
internal static EmptyResult Instance
{
get { return _singleton; }
}
public async Task ExecuteResultAsync(ControllerContext context)
{
}
}
}

View File

@ -0,0 +1,19 @@
using System.Threading.Tasks;
namespace Microsoft.AspNet.Mvc
{
public class HttpStatusCodeResult : IActionResult
{
private int _statusCode;
public HttpStatusCodeResult(int statusCode)
{
_statusCode = statusCode;
}
public async Task ExecuteResultAsync(ControllerContext context)
{
context.HttpContext.Response.StatusCode = _statusCode;
}
}
}

View File

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace Microsoft.AspNet.Mvc
{
public interface IActionResult
{
Task ExecuteResultAsync(ControllerContext context);
}
}

View File

@ -1,10 +0,0 @@
using System.Threading.Tasks;
using Microsoft.Owin;
namespace Microsoft.AspNet.Mvc
{
public interface IController
{
Task Execute(IOwinContext context);
}
}

View File

@ -40,14 +40,17 @@
<Reference Include="System.Core" /> <Reference Include="System.Core" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ContentResult.cs" />
<Compile Include="EmptyResult.cs" />
<Compile Include="IActionResult.cs" />
<Compile Include="Controller.cs" /> <Compile Include="Controller.cs" />
<Compile Include="ControllerActionInvoker.cs" /> <Compile Include="ControllerActionInvoker.cs" />
<Compile Include="ControllerActionInvokerFactory.cs" /> <Compile Include="ControllerActionInvokerFactory.cs" />
<Compile Include="ControllerContext.cs" /> <Compile Include="ControllerContext.cs" />
<Compile Include="DefaultControllerFactory.cs" /> <Compile Include="DefaultControllerFactory.cs" />
<Compile Include="HttpStatusCodeResult.cs" />
<Compile Include="IActionInvoker.cs" /> <Compile Include="IActionInvoker.cs" />
<Compile Include="IActionInvokerFactory.cs" /> <Compile Include="IActionInvokerFactory.cs" />
<Compile Include="IController.cs" />
<Compile Include="IControllerFactory.cs" /> <Compile Include="IControllerFactory.cs" />
<Compile Include="MvcHandler.cs" /> <Compile Include="MvcHandler.cs" />
<Compile Include="MvcServices.cs" /> <Compile Include="MvcServices.cs" />

View File

@ -8,5 +8,13 @@ namespace MvcSample
{ {
return "Hello World"; return "Hello World";
} }
public IActionResult Something()
{
return new ContentResult
{
Content = "Hello World"
};
}
} }
} }

View File

@ -12,6 +12,8 @@ namespace MvcSample
{ {
public void Configuration(IAppBuilder app) public void Configuration(IAppBuilder app)
{ {
app.UseErrorPage();
var handler = new MvcHandler(); var handler = new MvcHandler();
// Pretending to be routing // Pretending to be routing