Added basic HttpRequestMessage and HttpResponseMessage.

This commit is contained in:
David Fowler 2013-12-12 12:37:06 -08:00
parent 485eb48114
commit 1dcafe5df4
13 changed files with 133 additions and 34 deletions

View File

@ -0,0 +1,34 @@
using System.Net.Http;
using System.Net.Http.Formatting;
namespace Microsoft.AspNet.Mvc
{
public class ActionResultFactory : IActionResultFactory
{
public IActionResult CreateActionResult(object actionReturnValue)
{
var actionResult = actionReturnValue as IActionResult;
if (actionResult != null)
{
return actionResult;
}
var responseMessage = actionReturnValue as HttpResponseMessage;
if (responseMessage != null)
{
return new HttpResponseMessageActionResult(responseMessage);
}
// all other object types are treated as an http response message action result
var content = new ObjectContent(actionReturnValue.GetType(),
actionReturnValue,
new JsonMediaTypeFormatter());
return new HttpResponseMessageActionResult(new HttpResponseMessage
{
Content = content
});
}
}
}

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Globalization;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -7,11 +6,13 @@ namespace Microsoft.AspNet.Mvc
{ {
public class ControllerActionInvoker : IActionInvoker public class ControllerActionInvoker : IActionInvoker
{ {
private ControllerContext _context; private readonly ControllerContext _context;
private readonly IActionResultFactory _actionResultFactory;
public ControllerActionInvoker(ControllerContext context) public ControllerActionInvoker(ControllerContext context, IActionResultFactory actionResultFactory)
{ {
_context = context; _context = context;
_actionResultFactory = actionResultFactory;
} }
public Task InvokeActionAsync(string actionName) public Task InvokeActionAsync(string actionName)
@ -23,31 +24,11 @@ namespace Microsoft.AspNet.Mvc
throw new InvalidOperationException(String.Format("Could not find action method '{0}'", actionName)); throw new InvalidOperationException(String.Format("Could not find action method '{0}'", actionName));
} }
object actionReturnValue = method.Invoke(_context.Controller, null); ; object actionReturnValue = method.Invoke(_context.Controller, null);
IActionResult actionResult = CreateResult(actionReturnValue); IActionResult actionResult = _actionResultFactory.CreateActionResult(actionReturnValue);
return actionResult.ExecuteResultAsync(_context); 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,15 +1,18 @@
using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Microsoft.AspNet.Mvc namespace Microsoft.AspNet.Mvc
{ {
public class ControllerActionInvokerFactory : IActionInvokerFactory public class ControllerActionInvokerFactory : IActionInvokerFactory
{ {
private readonly IActionResultFactory _actionResultFactory;
public ControllerActionInvokerFactory(IActionResultFactory actionResultFactory)
{
_actionResultFactory = actionResultFactory;
}
public IActionInvoker CreateInvoker(ControllerContext context) public IActionInvoker CreateInvoker(ControllerContext context)
{ {
return new ControllerActionInvoker(context); return new ControllerActionInvoker(context, _actionResultFactory);
} }
} }
} }

View File

@ -0,0 +1,38 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Mvc
{
public class HttpResponseMessageActionResult : IActionResult
{
public HttpResponseMessage ResponseMessage { get; set; }
public HttpResponseMessageActionResult(HttpResponseMessage responseMessage)
{
ResponseMessage = responseMessage;
}
public async Task ExecuteResultAsync(ControllerContext context)
{
var response = context.HttpContext.Response;
response.StatusCode = (int)ResponseMessage.StatusCode;
foreach (var responseHeader in ResponseMessage.Headers)
{
response.Headers.AppendValues(responseHeader.Key, responseHeader.Value.ToArray());
}
var content = ResponseMessage.Content;
if (content != null)
{
foreach (var responseHeader in content.Headers)
{
response.Headers.AppendValues(responseHeader.Key, responseHeader.Value.ToArray());
}
await content.CopyToAsync(response.Body);
}
}
}
}

View File

@ -0,0 +1,8 @@

namespace Microsoft.AspNet.Mvc
{
public interface IActionResultFactory
{
IActionResult CreateActionResult(object actionReturnValue);
}
}

View File

@ -33,16 +33,26 @@
<Reference Include="Microsoft.Owin"> <Reference Include="Microsoft.Owin">
<HintPath>..\packages\Microsoft.Owin.2.0.2\lib\net45\Microsoft.Owin.dll</HintPath> <HintPath>..\packages\Microsoft.Owin.2.0.2\lib\net45\Microsoft.Owin.dll</HintPath>
</Reference> </Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Owin"> <Reference Include="Owin">
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath> <HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.0.0\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ActionResultFactory.cs" />
<Compile Include="ActionResultHelper.cs" /> <Compile Include="ActionResultHelper.cs" />
<Compile Include="ContentResult.cs" /> <Compile Include="ContentResult.cs" />
<Compile Include="EmptyResult.cs" /> <Compile Include="EmptyResult.cs" />
<Compile Include="HttpResponseMessageActionResult.cs" />
<Compile Include="IActionResult.cs" /> <Compile Include="IActionResult.cs" />
<Compile Include="Controller.cs" /> <Compile Include="Controller.cs" />
<Compile Include="ControllerActionInvoker.cs" /> <Compile Include="ControllerActionInvoker.cs" />
@ -52,6 +62,7 @@
<Compile Include="HttpStatusCodeResult.cs" /> <Compile Include="HttpStatusCodeResult.cs" />
<Compile Include="IActionInvoker.cs" /> <Compile Include="IActionInvoker.cs" />
<Compile Include="IActionInvokerFactory.cs" /> <Compile Include="IActionInvokerFactory.cs" />
<Compile Include="IActionResultFactory.cs" />
<Compile Include="IActionResultHelper.cs" /> <Compile Include="IActionResultHelper.cs" />
<Compile Include="IControllerFactory.cs" /> <Compile Include="IControllerFactory.cs" />
<Compile Include="MvcHandler.cs" /> <Compile Include="MvcHandler.cs" />

View File

@ -1,6 +1,6 @@
using System; using System;
using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.CoreServices; using Microsoft.AspNet.CoreServices;
using Microsoft.Owin; using Microsoft.Owin;

View File

@ -17,6 +17,7 @@ namespace Microsoft.AspNet.Mvc
callback(typeof(IControllerFactory), typeof(DefaultControllerFactory)); callback(typeof(IControllerFactory), typeof(DefaultControllerFactory));
callback(typeof(IActionInvokerFactory), typeof(ControllerActionInvokerFactory)); callback(typeof(IActionInvokerFactory), typeof(ControllerActionInvokerFactory));
callback(typeof(IActionResultHelper), typeof(ActionResultHelper)); callback(typeof(IActionResultHelper), typeof(ActionResultHelper));
callback(typeof(IActionResultFactory), typeof(ActionResultFactory));
} }
} }
} }

View File

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.0.0" targetFramework="net45" />
<package id="Microsoft.Owin" version="2.0.2" targetFramework="net45" /> <package id="Microsoft.Owin" version="2.0.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" /> <package id="Owin" version="1.0" targetFramework="net45" />
</packages> </packages>

View File

@ -1,4 +1,5 @@
using Microsoft.AspNet.Mvc; using System.Net.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Owin; using Microsoft.Owin;
namespace MvcSample namespace MvcSample

View File

@ -1,4 +1,5 @@
using Microsoft.AspNet.Mvc; using System.Net.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Owin; using Microsoft.Owin;
namespace MvcSample namespace MvcSample
@ -27,5 +28,13 @@ namespace MvcSample
{ {
Context.Response.Write("Hello World raw"); Context.Response.Write("Hello World raw");
} }
public HttpResponseMessage Hello2()
{
var responseMessage = new HttpResponseMessage();
responseMessage.Content = new StringContent("Hello World");
return responseMessage;
}
} }
} }

View File

@ -46,11 +46,20 @@
<Reference Include="Microsoft.Owin.Diagnostics"> <Reference Include="Microsoft.Owin.Diagnostics">
<HintPath>..\packages\Microsoft.Owin.Diagnostics.2.0.2\lib\net40\Microsoft.Owin.Diagnostics.dll</HintPath> <HintPath>..\packages\Microsoft.Owin.Diagnostics.2.0.2\lib\net40\Microsoft.Owin.Diagnostics.dll</HintPath>
</Reference> </Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Owin"> <Reference Include="Owin">
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath> <HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.0.0\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="packages.config" /> <Content Include="packages.config" />

View File

@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.0.0" targetFramework="net45" />
<package id="Microsoft.Owin" version="2.0.2" targetFramework="net45" /> <package id="Microsoft.Owin" version="2.0.2" targetFramework="net45" />
<package id="Microsoft.Owin.Diagnostics" version="2.0.2" targetFramework="net45" /> <package id="Microsoft.Owin.Diagnostics" version="2.0.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" /> <package id="Owin" version="1.0" targetFramework="net45" />
<package id="OwinHost" version="2.0.2" targetFramework="net45" /> <package id="OwinHost" version="2.0.2" targetFramework="net45" />
</packages> </packages>