如何将简单的字符串发送到Web Api C#?

时间:2017-01-05 23:53:31

标签: c# asp.net-web-api postman

我有这个简单的动作

PS > $x = .\tests\Excerpt_From_UnicodeDataTxt.ps1 -SearchString "Quotation|Apostrophe" | 
    Where-Object {$_.Category -match 'Punctuation'}

PS > $x.Count
23

PS > $x

Char CodePoint Category                   Description                                       
---- --------- --------                   -----------                                       
   " U+0022    Po-OtherPunctuation        Quotation Mark                                    
   ' U+0027    Po-OtherPunctuation        Apostrophe                                        
   « U+00AB    Pi-InitialQuotePunctuation Left-Pointing Double Angle Quotation Mark         
   » U+00BB    Pf-FinalQuotePunctuation   Right-Pointing Double Angle Quotation Mark        
   ՚ U+055A    Po-OtherPunctuation        Armenian Apostrophe                               
   ‘ U+2018    Pi-InitialQuotePunctuation Left Single Quotation Mark                        
   ’ U+2019    Pf-FinalQuotePunctuation   Right Single Quotation Mark                       
   ‚ U+201A    Ps-OpenPunctuation         Single Low-9 Quotation Mark                       
   ‛ U+201B    Pi-InitialQuotePunctuation Single High-Reversed-9 Quotation Mark             
   “ U+201C    Pi-InitialQuotePunctuation Left Double Quotation Mark                        
   ” U+201D    Pf-FinalQuotePunctuation   Right Double Quotation Mark                       
   „ U+201E    Ps-OpenPunctuation         Double Low-9 Quotation Mark                       
   ‟ U+201F    Pi-InitialQuotePunctuation Double High-Reversed-9 Quotation Mark             
   ‹ U+2039    Pi-InitialQuotePunctuation Single Left-Pointing Angle Quotation Mark         
   › U+203A    Pf-FinalQuotePunctuation   Single Right-Pointing Angle Quotation Mark        
   ❮ U+276E    Ps-OpenPunctuation         Heavy Left-Pointing Angle Quotation Mark Ornament 
   ❯ U+276F    Pe-ClosePunctuation        Heavy Right-Pointing Angle Quotation Mark Ornament
   ⹂ U+2E42    Ps-OpenPunctuation         Undefined                                         
   〝 U+301D    Ps-OpenPunctuation         Reversed Double Prime Quotation Mark              
   〞 U+301E    Pe-ClosePunctuation        Double Prime Quotation Mark                       
   〟 U+301F    Pe-ClosePunctuation        Low Double Prime Quotation Mark                   
   " U+FF02    Po-OtherPunctuation        Fullwidth Quotation Mark                          
   ' U+FF07    Po-OtherPunctuation        Fullwidth Apostrophe                              

我正在尝试通过邮递员发帖,其中包含“这是一个简单的字符串”(注意我的文字将会更长,所以我想通过正文而不是查询)。

我收到此错误

public void Post([FromBody] string t)
{
    var test = t;
}

2 个答案:

答案 0 :(得分:0)

错误消息指出“...请求实体的媒体类型'text / plain'”。更改您的服务以接受它或要求Postman以不同的媒体类型(如JSON)发送它。后一个将是更容易的解决方案,因为它看起来像你正在使用标准的ASP.NET Web API,它们的默认值是JSON。

发送此数据:

{ "t": "this is a simple string" }

......你会很好。

答案 1 :(得分:0)

正如@Quality Catalyst所述,您可以更改请求内容类型并以json格式发送字符串。或者您可以添加text / plain media type formatter:

public class PlainTextMediaTypeFormatter : MediaTypeFormatter
{
    public PlainTextMediaTypeFormatter()
    {
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
    }

    public override bool CanReadType(Type type)
    {
        return type == typeof(string);
    }

    public override bool CanWriteType(Type type)
    {
        return false; // you can return true and override WriteToStreamAsync
    }

    public override Task<object> ReadFromStreamAsync(Type type,
        Stream readStream, HttpContent content, IFormatterLogger formatterLogger,
        CancellationToken cancellationToken)
    {
        var memoryStream = new MemoryStream();
        readStream.CopyTo(memoryStream);
        return Task.FromResult((object)Encoding.UTF8.GetString(memoryStream.ToArray()));
    }     
}

在WebApiConfig中注册此格式化程序:

config.Formatters.Add(new PlainTextMediaTypeFormatter());

之后,您将能够在请求正文中以纯文本形式发送字符串。