本文整理汇总了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));
}
示例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);
}
示例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);
}
}
示例4: PropertiesForm
public PropertiesForm(HttpClient client, Uri propertiesUri, MediaTypeFormatter formatter)
{
InitializeComponent();
Switch = client;
PropertiesUri = propertiesUri;
Formatter = formatter;
}
示例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();
}
示例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));
}
示例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);
}
示例8: WebApiConfigModule
public WebApiConfigModule(IDependencyResolver dependencyResolver,
HttpConfiguration apiConfig,
MediaTypeFormatter jsonFormatter)
{
_dependencyResolver = dependencyResolver;
_apiConfig = apiConfig;
_jsonFormatter = jsonFormatter;
}
示例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);
}
示例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));
}
}
示例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;
}
示例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;
}
示例13: ODataFormatterParameterBinding
public ODataFormatterParameterBinding(HttpParameterDescriptor descriptor, MediaTypeFormatter formatter)
: base(descriptor)
{
if (formatter == null)
{
throw Error.ArgumentNull("formatter");
}
_formatter = formatter;
}
示例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''.");
}
示例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);
}
}