本文整理汇总了C#中MediaTypeFormatterCollection类的典型用法代码示例。如果您正苦于以下问题:C# MediaTypeFormatterCollection类的具体用法?C# MediaTypeFormatterCollection怎么用?C# MediaTypeFormatterCollection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MediaTypeFormatterCollection类属于命名空间,在下文中一共展示了MediaTypeFormatterCollection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetFormatters
private static MediaTypeFormatterCollection GetFormatters(IEnumerable<MediaTypeFormatter> formatters)
{
var mediaTypeFormatters = new MediaTypeFormatterCollection();
foreach(var formatter in formatters)
mediaTypeFormatters.Add(formatter);
return mediaTypeFormatters;
}
示例2: TestWebApi
private static void TestWebApi()
{
Console.WriteLine();
Console.WriteLine("------------------------------------------------------------");
Console.WriteLine("Web API");
Console.WriteLine("------------------------------------------------------------");
var client = new HttpClient {BaseAddress = new Uri("http://localhost:14851/")};
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var mediaTypeFormatterCollection = new MediaTypeFormatterCollection();
ExecuteAction(NumberOfRequestsToSend + " requests to api/item/id", () =>
{
for (int i = 0; i < NumberOfRequestsToSend; i++)
{
var response = client.GetAsync("api/item/" + Guid.NewGuid()).Result;
if (response.IsSuccessStatusCode)
{
var item = response.Content.ReadAsAsync<Item>(mediaTypeFormatterCollection).Result;
if (item == null)
throw new Exception("Item not received.");
//Console.WriteLine("ItemID\t\t{0}\nDescription\t{1}\nModifiedAt\t{2}", item.ItemID, item.Description, item.ModifiedAt);
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
}
});
}
示例3: Constructor1_SetsDerivedFormatters
public void Constructor1_SetsDerivedFormatters()
{
// force to array to get stable instances
MediaTypeFormatter[] derivedFormatters = HttpTestData.DerivedFormatters.ToArray();
MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(derivedFormatters);
Assert.True(derivedFormatters.SequenceEqual(collection));
}
示例4: ConfigureFormatters
public static void ConfigureFormatters(MediaTypeFormatterCollection formatters)
{
formatters.Remove(formatters.XmlFormatter);
JsonSerializerSettings settings = formatters.JsonFormatter.SerializerSettings;
settings.Formatting = Formatting.None;
}
示例5: Constructor1SetsProperties
public void Constructor1SetsProperties()
{
// All combination of formatters presented to ctor should still set XmlFormatter
foreach (IEnumerable<MediaTypeFormatter> formatterCollection in HttpUnitTestDataSets.AllFormatterCollections)
{
MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(formatterCollection);
if (collection.OfType<XmlMediaTypeFormatter>().Any())
{
Assert.NotNull(collection.XmlFormatter);
}
else
{
Assert.Null(collection.XmlFormatter);
}
if (collection.OfType<JsonMediaTypeFormatter>().Any())
{
Assert.NotNull(collection.JsonFormatter);
}
else
{
Assert.Null(collection.JsonFormatter);
}
}
}
示例6: ActionInvokerComponent
public ActionInvokerComponent(AppFunc next, MediaTypeFormatterCollection formatters, IContentNegotiator contentNegotiator, IActionParameterBinder parameterBinder)
{
_next = next;
_formatters = formatters;
_contentNegotiator = contentNegotiator;
_actionParameterBinder = parameterBinder;
}
示例7: RegisterFormatters
public static void RegisterFormatters(MediaTypeFormatterCollection formatters)
{
formatters.Remove(formatters.XmlFormatter);
var json = formatters.JsonFormatter;
json.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
}
示例8: CamelCasePropertyNamesContractResolver
/// <summary>
/// Format json to camel case
/// </summary>
/// <param name="formatters"></param>
public static void CamelCasePropertyNamesContractResolver(MediaTypeFormatterCollection formatters)
{
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
//settings.Formatting = Formatting.Indented;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
示例9: ItunesRepository
public ItunesRepository()
{
client = new HttpClient();
client.MaxResponseContentBufferSize = 1000000; // 1MB
formatters = new MediaTypeFormatterCollection();
formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/javascript"));
}
示例10: HttpResponseErrorHandler
internal HttpResponseErrorHandler(IEnumerable<MediaTypeFormatter> formatters, Uri helpUri, bool includeExceptionDetail)
{
Fx.Assert(formatters != null, "The 'formatters' parameter should not be null.");
this.formatters = new MediaTypeFormatterCollection(formatters);
this.helpUri = helpUri;
this.includeExceptionDetail = includeExceptionDetail;
}
示例11: RegisterFormatters
public static void RegisterFormatters(MediaTypeFormatterCollection formatters)
{
var jsonFormatter = formatters.JsonFormatter;
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var jsonpFormatter = new JsonpMediaTypeFormatter(jsonFormatter);
formatters.Add(jsonpFormatter);
}
示例12: Configure
private static void Configure(MediaTypeFormatterCollection formatters, HttpConfiguration config)
{
var xml = config.Formatters.XmlFormatter;
formatters.Remove(xml);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
示例13: Constructor
public void Constructor()
{
MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
Assert.Equal(3, collection.Count);
Assert.NotNull(collection.XmlFormatter);
Assert.NotNull(collection.JsonFormatter);
Assert.NotNull(collection.FormUrlEncodedFormatter);
}
示例14: SetFormatters
private static void SetFormatters(MediaTypeFormatterCollection formatters)
{
formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Include;
}
示例15: MultipartMediaTypeFormatter
public MultipartMediaTypeFormatter([NotNull] MediaTypeFormatterCollection mediaTypeFormatterCollection,
[NotNull] IPropertyHelper propertyHelper)
{
if (mediaTypeFormatterCollection == null) throw new ArgumentNullException(nameof(mediaTypeFormatterCollection));
if (propertyHelper == null) throw new ArgumentNullException(nameof(propertyHelper));
_mediaTypeFormatterCollection = mediaTypeFormatterCollection;
_propertyHelper = propertyHelper;
SupportedMediaTypes.Add(new MediaTypeHeaderValue("multipart/form-data"));
}