WEBAPI Support for Text/plain requests

Source: Internet
Author: User

Today encountered a requirement, request to access my API with HTTPS + XML, ordinary webapi is not supported by this request, so do the following code to support

Add a class with the class name Plaintexttypeformatter

 Public classplaintexttypeformatter:mediatypeformatter{ PublicPlaintexttypeformatter () {Supportedmediatypes.add (NewMediatypeheadervalue ("Text/plain")); }     Public Override BOOLcanreadtype (Type type) {returnType = =typeof(string); }     Public Override BOOLcanwritetype (Type type) {returnType = =typeof(string); }     Public Override AsyncTask writetostreamasync (Type type,Objectvalue, Stream writestream, httpcontent content, TransportContext transportcontext) {        using(varSW =NewStreamWriter (Writestream)) {            awaitSW. WriteAsync (value.        ToString ()); }    }     Public Override Asynctask<Object>readfromstreamasync (Type type, Stream readstream, httpcontent content, Iformatterlogger Formatterlogger) { using(varSR =NewStreamReader (Readstream)) {            return awaitSr.        Readtoendasync (); }    }}

Then register the class to Forrmator, (add the following code to the Webapiconfig)

Config. Formatters.add (new plaintexttypeformatter ());

Then do not forget to add the header at the request, otherwise the request cannot be processed

Content-type:text/plain

The following is the request result in fiddler

WEBAPI Support for Text/plain requests

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.