我想要一個自定義屬性來將數據解析爲流並且可以使用Swagger進行測試。Swagger不識別具有自定義屬性的WebAPI控制器參數[FromContent]
因此,我創建控制器從POST
體寫着:
[SwaggerOperation("Create")]
[SwaggerResponse(HttpStatusCode.Created)]
public async Task<string> Post([FromContent]Stream contentStream)
{
using (StreamReader reader = new StreamReader(contentStream, Encoding.UTF8))
{
var str = reader.ReadToEnd();
Console.WriteLine(str);
}
return "OK";
}
如何定義數據流,因此在揚鞭UI是可見的?
這是我實現FromContent
屬性和ContentParameterBinding
結合:
public class ContentParameterBinding : HttpParameterBinding
{
private struct AsyncVoid{}
public ContentParameterBinding(HttpParameterDescriptor descriptor) : base(descriptor)
{
}
public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider,
HttpActionContext actionContext,
CancellationToken cancellationToken)
{
var binding = actionContext.ActionDescriptor.ActionBinding;
if (binding.ParameterBindings.Length > 1 ||
actionContext.Request.Method == HttpMethod.Get)
{
var taskSource = new TaskCompletionSource<AsyncVoid>();
taskSource.SetResult(default(AsyncVoid));
return taskSource.Task as Task;
}
var type = binding.ParameterBindings[0].Descriptor.ParameterType;
if (type == typeof(HttpContent))
{
SetValue(actionContext, actionContext.Request.Content);
var tcs = new TaskCompletionSource<object>();
tcs.SetResult(actionContext.Request.Content);
return tcs.Task;
}
if (type == typeof(Stream))
{
return actionContext.Request.Content
.ReadAsStreamAsync()
.ContinueWith((task) =>
{
SetValue(actionContext, task.Result);
});
}
throw new InvalidOperationException("Only HttpContent and Stream are supported for [FromContent] parameters");
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
public sealed class FromContentAttribute : ParameterBindingAttribute
{
public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter)
{
if (parameter == null)
throw new ArgumentException("Invalid parameter");
return new ContentParameterBinding(parameter);
}
}
更新
當我創建Stream
使用[FromBody]
是顯示正確揚鞭,但Stream
沒有啓動, ==null
[SwaggerOperation("Create")]
[SwaggerResponse(HttpStatusCode.Created)]
public async Task<string> Post([FromBody]Stream contentStream)
{
using (StreamReader reader = new StreamReader(contentStream, Encoding.UTF8))
{
var str = reader.ReadToEnd();
Console.WriteLine(str);
}
return "OK";
}
所以我想有相同的用戶界面,但與我的自定義屬性這讓是我有從內容Stream
。
隨着我的自定義屬性就說明沒有TextArea
的參數,但可以用郵差進行測試和正常工作和Stream
可
你如何想展示它?哪裏有問題?你應該澄清什麼輸出你期望 – Marusyk
@MegaTron我試圖添加測試的帖子,其中流已經從內容正文創建,我會添加示例和圖片 –
整個參數綁定的東西似乎是一個很大的工作避免在控制器方法中執行'var stream = await this.Request.Content.ReadAsStreamAsync()'。 –