diff --git a/Microsoft.AspNet.Mvc/ContentResult.cs b/Microsoft.AspNet.Mvc/ContentResult.cs new file mode 100644 index 0000000000..825fdc219e --- /dev/null +++ b/Microsoft.AspNet.Mvc/ContentResult.cs @@ -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); + } + } + } +} diff --git a/Microsoft.AspNet.Mvc/ControllerActionInvoker.cs b/Microsoft.AspNet.Mvc/ControllerActionInvoker.cs index 1b9e1f0ce4..01b9b0bc33 100644 --- a/Microsoft.AspNet.Mvc/ControllerActionInvoker.cs +++ b/Microsoft.AspNet.Mvc/ControllerActionInvoker.cs @@ -1,7 +1,6 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Globalization; +using System.Reflection; using System.Threading.Tasks; namespace Microsoft.AspNet.Mvc @@ -17,16 +16,38 @@ namespace Microsoft.AspNet.Mvc 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) { 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(); } } } diff --git a/Microsoft.AspNet.Mvc/DefaultControllerFactory.cs b/Microsoft.AspNet.Mvc/DefaultControllerFactory.cs index fe4dfca643..940e7026f0 100644 --- a/Microsoft.AspNet.Mvc/DefaultControllerFactory.cs +++ b/Microsoft.AspNet.Mvc/DefaultControllerFactory.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using System.Reflection; using Microsoft.AspNet.CoreServices; using Microsoft.Owin; @@ -17,6 +16,11 @@ namespace Microsoft.AspNet.Mvc public object CreateController(IOwinContext context, string controllerName) { + if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)) + { + controllerName += "Controller"; + } + foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) { var type = a.GetType(controllerName) ?? diff --git a/Microsoft.AspNet.Mvc/EmptyResult.cs b/Microsoft.AspNet.Mvc/EmptyResult.cs new file mode 100644 index 0000000000..8fb3b3c890 --- /dev/null +++ b/Microsoft.AspNet.Mvc/EmptyResult.cs @@ -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) + { + } + } +} diff --git a/Microsoft.AspNet.Mvc/HttpStatusCodeResult.cs b/Microsoft.AspNet.Mvc/HttpStatusCodeResult.cs new file mode 100644 index 0000000000..2e0b21f657 --- /dev/null +++ b/Microsoft.AspNet.Mvc/HttpStatusCodeResult.cs @@ -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; + } + } +} diff --git a/Microsoft.AspNet.Mvc/IActionResult.cs b/Microsoft.AspNet.Mvc/IActionResult.cs new file mode 100644 index 0000000000..ac6ef59247 --- /dev/null +++ b/Microsoft.AspNet.Mvc/IActionResult.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Microsoft.AspNet.Mvc +{ + public interface IActionResult + { + Task ExecuteResultAsync(ControllerContext context); + } +} diff --git a/Microsoft.AspNet.Mvc/IController.cs b/Microsoft.AspNet.Mvc/IController.cs deleted file mode 100644 index 69ceefd322..0000000000 --- a/Microsoft.AspNet.Mvc/IController.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.Owin; - -namespace Microsoft.AspNet.Mvc -{ - public interface IController - { - Task Execute(IOwinContext context); - } -} diff --git a/Microsoft.AspNet.Mvc/Microsoft.AspNet.Mvc.csproj b/Microsoft.AspNet.Mvc/Microsoft.AspNet.Mvc.csproj index 6aa3dbc0a7..3be7e25b31 100644 --- a/Microsoft.AspNet.Mvc/Microsoft.AspNet.Mvc.csproj +++ b/Microsoft.AspNet.Mvc/Microsoft.AspNet.Mvc.csproj @@ -40,14 +40,17 @@ + + + + - diff --git a/MvcSample/HomeController.cs b/MvcSample/HomeController.cs index 8d4fa21623..2e38aa8384 100644 --- a/MvcSample/HomeController.cs +++ b/MvcSample/HomeController.cs @@ -8,5 +8,13 @@ namespace MvcSample { return "Hello World"; } + + public IActionResult Something() + { + return new ContentResult + { + Content = "Hello World" + }; + } } } \ No newline at end of file diff --git a/MvcSample/Startup.cs b/MvcSample/Startup.cs index 42bff2320f..216d97a5a4 100644 --- a/MvcSample/Startup.cs +++ b/MvcSample/Startup.cs @@ -12,6 +12,8 @@ namespace MvcSample { public void Configuration(IAppBuilder app) { + app.UseErrorPage(); + var handler = new MvcHandler(); // Pretending to be routing