当前位置: 首页>>代码示例>>C#>>正文


C# Formatting.MediaTypeFormatter类代码示例

本文整理汇总了C#中System.Net.Http.Formatting.MediaTypeFormatter的典型用法代码示例。如果您正苦于以下问题:C# MediaTypeFormatter类的具体用法?C# MediaTypeFormatter怎么用?C# MediaTypeFormatter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


MediaTypeFormatter类属于System.Net.Http.Formatting命名空间,在下文中一共展示了MediaTypeFormatter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: JsonpMediaTypeFormatter

        /// <summary>
        /// Initializes a new instance of the <see cref="JsonpMediaTypeFormatter"/> class.
        /// </summary>
        /// <param name="jsonMediaTypeFormatter">The <see cref="JsonMediaTypeFormatter"/> to use internally for JSON serialization.</param>
        /// <param name="callbackQueryParameter">The query parameter containing the callback.</param>
        public JsonpMediaTypeFormatter(MediaTypeFormatter jsonMediaTypeFormatter, string callbackQueryParameter = "callback")
        {
            if (jsonMediaTypeFormatter == null)
            {
                throw new ArgumentNullException("jsonMediaTypeFormatter");
            }

            if (callbackQueryParameter == null)
            {
                throw new ArgumentNullException("callbackQueryParameter");
            }

            _jsonMediaTypeFormatter = jsonMediaTypeFormatter;
            _callbackQueryParameter = callbackQueryParameter;

            SupportedMediaTypes.Add(_textJavaScript);
            SupportedMediaTypes.Add(_applicationJavaScript);
            SupportedMediaTypes.Add(_applicationJsonp);
            foreach (var encoding in _jsonMediaTypeFormatter.SupportedEncodings)
            {
                SupportedEncodings.Add(encoding);
            }
                
            MediaTypeMappings.Add(new QueryStringMapping("format", "jsonp", _textJavaScript));
            MediaTypeMappings.Add(new UriPathExtensionMapping("jsonp", _textJavaScript));
        }
开发者ID:panesofglass,项目名称:WebApiContrib.Formatting.Jsonp,代码行数:31,代码来源:JsonpMediaTypeFormatter.cs

示例2: EntryEndpoint

 /// <summary>
 /// Creates a new entry point using an OAuth token.
 /// </summary>
 /// <param name="uri">The base URI of the REST interface. Missing trailing slash will be appended automatically.</param>
 /// <param name="token">The OAuth token to present as a "Bearer" to the REST interface.</param>
 /// <param name="serializer">Controls the serialization of entities sent to and received from the server. Defaults to a JSON serializer if unset.</param>
 public EntryEndpoint(Uri uri, string token, MediaTypeFormatter serializer = null) : base(
     uri: uri.EnsureTrailingSlash(),
     httpClient: BuildHttpClient(uri),
     serializer: serializer ?? BuildSerializer())
 {
     HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
 }
开发者ID:1and1,项目名称:TypedRest-DotNet,代码行数:13,代码来源:EntryEndpoint.cs

示例3: Bind

        private void Bind(string methodName)
        {
            //创建FormatterParameterBinding对象
            MethodInfo method = typeof(ContactsController).GetMethod(methodName);
            HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(this.ControllerContext.ControllerDescriptor, method);
            HttpParameterDescriptor parameterDescriptor = actionDescriptor.GetParameters().First();
            MediaTypeFormatter[] formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };
            FormatterParameterBinding parameterBinding = new FormatterParameterBinding(parameterDescriptor, formatters, null);

            //创建HttpActionBinding并执行
            HttpActionBinding actionBinding = new HttpActionBinding(actionDescriptor,new FormatterParameterBinding[] { parameterBinding });
            HttpActionContext actionContext =new HttpActionContext(this.ControllerContext, actionDescriptor);
            try
            {
                actionBinding.ExecuteBindingAsync(actionContext, CancellationToken.None).Wait();

                //获取绑定参数对象并打印相关数据
                Contact contact = (Contact)actionContext.ActionArguments["contact"];
                Console.WriteLine("{0,-12}: {1}", "Name", contact.Name);
                Console.WriteLine("{0,-12}: {1}", "Phone No.", contact.PhoneNo);
                Console.WriteLine("{0,-12}: {1}", "EmailAddress", contact.EmailAddress);
                Console.WriteLine("{0,-12}: {1}", "Address", contact.Address);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
开发者ID:chenboyi081,项目名称:asp-net-web-api-2-samples,代码行数:28,代码来源:ContactsController.cs

示例4: PropertiesForm

 public PropertiesForm(HttpClient client, Uri propertiesUri, MediaTypeFormatter formatter)
 {
     InitializeComponent();
     Switch = client;
     PropertiesUri = propertiesUri;
     Formatter = formatter;
 }
开发者ID:RadishSystems,项目名称:choiceview-webapitester-csharp,代码行数:7,代码来源:PropertiesForm.cs

示例5: PseudoMediaTypeFormatter

        public PseudoMediaTypeFormatter(MediaTypeFormatter referFormatter = null)
        {
            SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true));
            SupportedEncodings.Add(new UnicodeEncoding(bigEndian: false, byteOrderMark: true, throwOnInvalidBytes: true));

            _ReferFormatter = referFormatter ?? new JsonMediaTypeFormatter();
        }
开发者ID:REALTOBIZ,项目名称:DbWebApi,代码行数:7,代码来源:PseudoMediaTypeFormatter.cs

示例6: AddSupportedEncodings

 private static void AddSupportedEncodings(MediaTypeFormatter formatter)
 {
     formatter.SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false,
         throwOnInvalidBytes: true));
     formatter.SupportedEncodings.Add(new UnicodeEncoding(bigEndian: false, byteOrderMark: true,
         throwOnInvalidBytes: true));
 }
开发者ID:brianly,项目名称:aspnetwebstack,代码行数:7,代码来源:ODataMediaTypeFormatters.cs

示例7: CreateXmlMediaTypes

 private static void CreateXmlMediaTypes(MediaTypeFormatter xmlFormatter)
 {
     var mediaTypes = new List<string> {
         "application/vnd.bekk.kh.v1+xml",
         "application/vnd.bekk.kh.v2+xml"
     };
     CreateMediaTypes(xmlFormatter, mediaTypes);
 }
开发者ID:espenekvang,项目名称:VersionMe,代码行数:8,代码来源:WebApiConfig.cs

示例8: WebApiConfigModule

 public WebApiConfigModule(IDependencyResolver dependencyResolver,
                           HttpConfiguration apiConfig,
                           MediaTypeFormatter jsonFormatter)
 {
     _dependencyResolver = dependencyResolver;
     _apiConfig = apiConfig;
     _jsonFormatter = jsonFormatter;
 }
开发者ID:andycmaj,项目名称:SixDegreesApi,代码行数:8,代码来源:WebApiConfigModule.cs

示例9: ReadAsAsyncOfT_WhenTypeIsReferenceTypeAndNoMediaType_ReturnsNull

        public void ReadAsAsyncOfT_WhenTypeIsReferenceTypeAndNoMediaType_ReturnsNull()
        {
            var content = new StringContent("{}");
            content.Headers.ContentType = null;
            var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };

            Assert.Equal(null, content.ReadAsAsync<List<string>>(formatters).Result);
        }
开发者ID:marojeri,项目名称:aspnetwebstack,代码行数:8,代码来源:HttpContentExtensionsTest.cs

示例10: CreateMediaTypes

 private static void CreateMediaTypes(MediaTypeFormatter mediaTypeFormatter, IEnumerable<string> mediaTypes)
 {
     //Vendor specific media types http://www.iana.org/cgi-bin/mediatypes.pl
     foreach (var mediaType in mediaTypes)
     {
         mediaTypeFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue(mediaType));
     }
 }
开发者ID:espenekvang,项目名称:VersionMe,代码行数:8,代码来源:WebApiConfig.cs

示例11: CriarRequest

 protected HttpRequestMessage CriarRequest(string url, HttpMethod method, object content = null, string mediaType = "application/json", MediaTypeFormatter formatter = null)
 {
     var request = new HttpRequestMessage { RequestUri = new Uri(urlBase + url), Method = method };
     request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType));
     if (content!=null)
         request.Content = new ObjectContent(content.GetType(), content, formatter ?? new JsonMediaTypeFormatter());
     return request;
 }
开发者ID:giggio,项目名称:Exemplo-blog-teste-webapi,代码行数:8,代码来源:ControllerApiTesteBase.cs

示例12: CreateTracer

        public static MediaTypeFormatter CreateTracer(MediaTypeFormatter formatter, ITraceWriter traceWriter, HttpRequestMessage request)
        {
            // If we have been asked to wrap a tracer around a formatter, it could be
            // already wrapped, and there is nothing to do.  But if we see it is a tracer
            // that is not associated with a request, we wrap it into a new tracer that
            // does have a request.  The only formatter tracers without requests are the
            // ones in the default MediaTypeFormatterCollection in the HttpConfiguration.
            IFormatterTracer formatterTracer = formatter as IFormatterTracer;
            if (formatterTracer != null)
            {
                if (formatterTracer.Request == request)
                {
                    return formatter;
                }

                formatter = formatterTracer.InnerFormatter;
            }

            MediaTypeFormatter tracer = null;

            // We special-case Xml, Json and FormUrlEncoded formatters because we expect to be able
            // to find them with IsAssignableFrom in the MediaTypeFormatterCollection.
            if (formatter is XmlMediaTypeFormatter)
            {
                tracer = new XmlMediaTypeFormatterTracer(formatter, traceWriter, request);
            }
            else if (formatter is JsonMediaTypeFormatter)
            {
                tracer = new JsonMediaTypeFormatterTracer(formatter, traceWriter, request);
            }
            else if (formatter is FormUrlEncodedMediaTypeFormatter)
            {
                tracer = new FormUrlEncodedMediaTypeFormatterTracer(formatter, traceWriter, request);
            }
            else if (formatter is BufferedMediaTypeFormatter)
            {
                tracer = new BufferedMediaTypeFormatterTracer(formatter, traceWriter, request);
            }
            else
            {
                tracer = new MediaTypeFormatterTracer(formatter, traceWriter, request);
            }

            // Copy SupportedMediaTypes and MediaTypeMappings because they are publically visible
            tracer.SupportedMediaTypes.Clear();
            foreach (MediaTypeHeaderValue mediaType in formatter.SupportedMediaTypes)
            {
                tracer.SupportedMediaTypes.Add(mediaType);
            }

            tracer.MediaTypeMappings.Clear();
            foreach (MediaTypeMapping mapping in formatter.MediaTypeMappings)
            {
                tracer.MediaTypeMappings.Add(mapping);
            }

            return tracer;
        }
开发者ID:haoduotnt,项目名称:aspnetwebstack,代码行数:58,代码来源:MediaTypeFormatterTracer.cs

示例13: ODataFormatterParameterBinding

 public ODataFormatterParameterBinding(HttpParameterDescriptor descriptor, MediaTypeFormatter formatter)
     : base(descriptor)
 {
     if (formatter == null)
     {
         throw Error.ArgumentNull("formatter");
     }
     _formatter = formatter;
 }
开发者ID:marojeri,项目名称:aspnetwebstack,代码行数:9,代码来源:ODataFormatterParameterBinding.cs

示例14: ReadAsAsyncOfT_WhenNoMatchingFormatterFoundForContentWithNoMediaType_Throws

        public void ReadAsAsyncOfT_WhenNoMatchingFormatterFoundForContentWithNoMediaType_Throws()
        {
            var content = new StringContent("{}");
            content.Headers.ContentType = null;
            var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };

            Assert.Throws<InvalidOperationException>(() => content.ReadAsAsync<List<string>>(formatters),
                "No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type ''undefined''.");
        }
开发者ID:sanyaade-mobiledev,项目名称:aspnetwebstack-1,代码行数:9,代码来源:HttpContentExtensionsTest.cs

示例15: Post

 public void Post()
 {
     IEnumerable<MediaTypeFormatter> formatters = new MediaTypeFormatter[] { new FormUrlEncodedMediaTypeFormatter() };
     FormDataCollection formData = this.Request.Content.ReadAsAsync<FormDataCollection>(formatters).Result;
     foreach (var item in formData)
     {
         Console.WriteLine("{0,-12}: {1}", item.Key, item.Value);
     }
 }
开发者ID:chenboyi081,项目名称:asp-net-web-api-2-samples,代码行数:9,代码来源:ContactsController.cs


注:本文中的System.Net.Http.Formatting.MediaTypeFormatter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。