how to post plain text to ASP.NET Web API endpoint?

前端 未结 6 2234
时光取名叫无心
时光取名叫无心 2020-12-04 17:34

I have an ASP.NET Web API endpoint with controller action defined as follows :

[HttpPost]
public HttpResponseMessage Post([FromBody] object text)

6条回答
  •  执念已碎
    2020-12-04 18:24

    Since Web API doesn't have out of box formatter for handling text/plain, some options:

    1. Modify your action to have no parameters... reason is having parameters triggers request body de-serialization. Now you can read the request content explicitly by doing await Request.Content.ReadAsStringAsync() to get the string

    2. Write a custom MediaTypeFormatter to handle 'text/plain'... it's actually simple to write in this case and you could keep the parameters on the action.

提交回复
热议问题