简体   繁体   中英

Using c# web api with alternate content-type

I'm new to web api and I need to create a server for a client. I have no control over the client - can't change a thing.

The client sends in an html encapsulated json request in a POST body. However, the content-type can vary. What do I need to do to allow my ApiController to process different content-types?

Under the hood, Web Api supports Content Negotiation mechanism to automatically opt the correct formatter based on the header Content-Type in HTTP request.

By default content negotiation supports three formatters: json , xml and form-urlencoded data . If no formatter found, client will receives HTTP error 406 (Not Acceptable).

See more:

https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/content-negotiation

If you need to allow Web Api support another Content-Type , you can write your own custom formatter:

https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/media-formatters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM