我有一个API定义接口(合同)
public interface IGuidelineApi : IBaseApi
{
ApiResult Operation(int i, string s, long l);
}
和Server Side API实现原型是这样的:
[Route("api/[controller]")]
public class GuidelineController : IGuidelineApi
{
[HttpGet]
public ApiResult Operation([FromQuery]int i, [FromQuery]string s, [FromQuery]long l)
{
// ApiResult inherits ActionResult
return new ApiResult(new { intVal = i, strVal = s, longVal = l });
}
}
从任何使用DispatchProxy的客户端(MVC客户端应用程序或移动设备)执行任何API方法可以通过(API接口)依赖注入调用它们
private readonly IGuidelineApi _guidelineApi;
public AccountController(IGuidelineApi guidelineApi)
{
_guidelineApi = guidelineApi;
// DispatchProxy magic here!
_guidelineApi.Operation(1, "hello", 617);
}
所以问题是,我可以直接将API响应传输到MVC客户端输出(IActionResult)而不进行序列化。 尝试直接替换 - 覆盖实际响应(客户端的StreamContent)
答案 0 :(得分:0)
如果你知道结果类型,这项工作:
API调用结果执行演示:
// Direct stream transport
if (response.Content is StreamContent)
{
actionContext.HttpContext.Response.ContentType = "application/json; charset=utf-8";
actionContext.HttpContext.Response.Headers.Add("Metadata : Type.Sample.DataSourceResult");
await ((StreamContent)response.Content).CopyToAsync(actionContext.HttpContext.Response.Body)
// Global Action Filter must know actual result
return null
}
MVC注册全球行动过滤器:
public override void OnActionExecuted(ActionExecutedContext context)
{
// Header check with extension method for type info
if (context.HttpContext.Response.IsDataSourceResult())
{
// prevent end of null or empty string character!
context.Result = new NoContentResult();
}
}