Webapi,webnet receiving JSONP

Source: Internet
Author: User

This essay is reproduced in the article, do not laugh

The first step:

Create Jsonpmediatypeformatter

  Public classJsonpmediatypeformatter:jsonmediatypeformatter { Public stringCallback {Get;Private Set; }  PublicJsonpmediatypeformatter (stringcallback =NULL)        {             This. Callback =callback; }         Public OverrideTask writetostreamasync (Type type,Objectvalue, Stream writestream, httpcontent content, TransportContext transportcontext) {            if(string. IsNullOrEmpty ( This. Callback)) {return Base.            Writetostreamasync (type, value, Writestream, content, TransportContext); }            Try            {                 This.                WriteToStream (type, value, writestream, content); returnTask.fromresult<asyncvoid> (Newasyncvoid ()); }            Catch(Exception Exception) {TaskCompletionSource<AsyncVoid> Source =NewTaskcompletionsource<asyncvoid>(); Source.                SetException (Exception); returnsource.            Task; }        }        Private voidWriteToStream (Type type,Objectvalue, Stream writestream, httpcontent content) {Jsonserializer Serializer= Jsonserializer.create ( This.            Serializersettings); using(StreamWriter StreamWriter =NewStreamWriter (Writestream, This. Supportedencodings.first ()))using(JsonTextWriter JsonTextWriter =NewJsonTextWriter (streamWriter) {closeoutput =false}) {Jsontextwriter.writeraw ( This. Callback +"("); Serializer.                Serialize (jsontextwriter, value); Jsontextwriter.writeraw (")"); }        }         Public Overridemediatypeformatter getperrequestformatterinstance (type type, httprequestmessage request, Mediatypeheadervalue MediaType) {if(Request. Method! =httpmethod.get) {return  This; }            stringcallback; if(Request. Getquerynamevaluepairs (). ToDictionary (pair =pair. Key, Pair= = Pair. Value). TryGetValue ("Callback", outcallback)) {                return NewJsonpmediatypeformatter (callback); }            return  This; } [StructLayout (LayoutKind.Sequential, Size=1)]        Private structasyncvoid {}}

Step Two:

Global.asax Add the following code

protected void Application_Start ()        {
       //Return JSON when used GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear (); // To set the JSONP format serialization method GlobalConfiguration.Configuration.Formatters.Insert (0, new jsonpmediatypeformatter ()); Arearegistration.registerallareas (); Globalconfiguration.configure (webapiconfig.register); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles); }

Webapi,webnet receive JSONP

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.