Skip to content

Instantly share code, notes, and snippets.

@mahizsas
Created December 13, 2013 11:55
Show Gist options
  • Save mahizsas/7943240 to your computer and use it in GitHub Desktop.
Save mahizsas/7943240 to your computer and use it in GitHub Desktop.
/// <summary>
/// Reads the Request body into a string/byte[] and
/// assigns it to the parameter bound.
///
/// Should only be used with a single parameter on
/// a Web API method using the [NakedBody] attribute
/// </summary>
public class NakedBodyParameterBinding : HttpParameterBinding
{
public NakedBodyParameterBinding(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)
return EmptyTask.Start();
var type = binding
.ParameterBindings[0]
.Descriptor.ParameterType;
if (type == typeof(string))
{
return actionContext.Request.Content
.ReadAsStringAsync()
.ContinueWith((task) =>
{
var stringResult = task.Result;
SetValue(actionContext, stringResult);
});
}
else if (type == typeof(byte[]))
{
return actionContext.Request.Content
.ReadAsByteArrayAsync()
.ContinueWith((task) =>
{
byte[] result = task.Result;
SetValue(actionContext, result);
});
}
throw new InvalidOperationException("Only string and byte[] are supported for [NakedBody] parameters");
}
public override bool WillReadBody
{
get
{
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment