Как я помню, ASP.NET WebAPI не может обрабатывать text/plain
тип мультимедиа, поэтому, если вы отправляете только строку, ваша конечная точка получает пустую строку, вы можете попытаться установить другой заголовок Content-Type в запросе,или, если вы хотите добавить поддержку к типу контента text/plain
, вы можете следовать этому руководству .
public class PlainTextMediaTypeFormatter : MediaTypeFormatter
{
public PlainTextMediaTypeFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
}
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
var source = new TaskCompletionSource<object>();
try
{
using (var memoryStream = new MemoryStream())
{
readStream.CopyTo(memoryStream);
var text = Encoding.UTF8.GetString(memoryStream.ToArray());
source.SetResult(text);
}
}
catch (Exception e)
{
source.SetException(e);
}
return source.Task;
}
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, System.Net.TransportContext transportContext, System.Threading.CancellationToken cancellationToken)
{
var bytes = Encoding.UTF8.GetBytes(value.ToString());
return writeStream.WriteAsync(bytes, 0, bytes.Length, cancellationToken);
}
public override bool CanReadType(Type type)
{
return type == typeof(string);
}
public override bool CanWriteType(Type type)
{
return type == typeof(string);
}
}
Затем его можно добавить в коллекцию config.Formatters:
public static class WebApiConfig
{
public static void Register(HttpConfiguration http)
{
http.Formatters.Add(new PlainTextMediaTypeFormatter());
}
}