本文整理汇总了C#中System.Net.Http.ObjectContent类的典型用法代码示例。如果您正苦于以下问题:C# ObjectContent类的具体用法?C# ObjectContent怎么用?C# ObjectContent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ObjectContent类属于System.Net.Http命名空间,在下文中一共展示了ObjectContent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetText
public HttpResponseMessage GetText(string type, [FromBody] string inputText)
{
// if string is empty
if (string.IsNullOrWhiteSpace(inputText))
{
return Request.CreateResponse(HttpStatusCode.NoContent, "Empty string");
}
// save to file
Func.SaveToFile(inputText);
// parse into revalent models
Text text = Parser.ParseInputText(inputText);
// get formatter from type
var formatter = FormatFactory.GetFormatter(type);
// create respons content
var content = new ObjectContent<Text>(
text, // What we are serializing
formatter//, // The media formatter
//mediaTypeHeaderValue.MediaType // The MIME (multimedia internet message exchange )type
);
return new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content = content
};
}
示例2: ContainsFormatters
public static void ContainsFormatters(ObjectContent objectContent, IEnumerable<MediaTypeFormatter> formatters)
{
Assert.IsNotNull(objectContent, "objectContent cannot be null.");
Assert.IsNotNull(formatters, "Test error: formatters must be specified.");
Assert.IsNotNull(objectContent.Formatters, "Formatters property cannot be null.");
CollectionAssert.IsSubsetOf(formatters.ToList(), objectContent.Formatters, "Formatters did not include all expected formatters.");
}
示例3: ArrayOfIntsSerializesAsOData
private void ArrayOfIntsSerializesAsOData(string expectedContent, bool json)
{
ObjectContent<int[]> content = new ObjectContent<int[]>(new int[] { 10, 20, 30, 40, 50 }, _formatter,
GetMediaType(json));
AssertEqual(json, expectedContent, content.ReadAsStringAsync().Result);
}
示例4: IEnumerableOfEntityTypeSerializesAsODataFeed
private void IEnumerableOfEntityTypeSerializesAsODataFeed(string expectedContent, bool json)
{
ODataMediaTypeFormatter formatter = CreateFormatter();
IEnumerable<Employee> collectionOfPerson = new Collection<Employee>()
{
(Employee)TypeInitializer.GetInstance(SupportedTypes.Employee, 0),
(Employee)TypeInitializer.GetInstance(SupportedTypes.Employee, 1),
};
ObjectContent<IEnumerable<Employee>> content = new ObjectContent<IEnumerable<Employee>>(collectionOfPerson,
formatter, json ? ODataMediaTypes.ApplicationJsonODataMinimalMetadata :
ODataMediaTypes.ApplicationAtomXmlTypeFeed);
string actualContent = content.ReadAsStringAsync().Result;
if (json)
{
JsonAssert.Equal(expectedContent, actualContent);
}
else
{
RegexReplacement replaceUpdateTime = new RegexReplacement(
"<updated>*.*</updated>", "<updated>UpdatedTime</updated>");
Assert.Xml.Equal(expectedContent, actualContent, replaceUpdateTime);
}
}
示例5: ArrayOfBoolsSerializesAsOData
private void ArrayOfBoolsSerializesAsOData(string expectedContent, bool json)
{
ObjectContent<bool[]> content = new ObjectContent<bool[]>(new bool[] { true, false, true, false },
_formatter, GetMediaType(json));
AssertEqual(json, expectedContent, content.ReadAsStringAsync().Result);
}
示例6: ContentHeadersAreAddedForXmlMediaType
public void ContentHeadersAreAddedForXmlMediaType()
{
ObjectContent<IEnumerable<Person>> content = new ObjectContent<IEnumerable<Person>>(new Person[] { new Person(0, new ReferenceDepthContext(7)) }, _formatter);
content.LoadIntoBufferAsync().Wait();
Assert.Http.Contains(content.Headers, "DataServiceVersion", "3.0;");
Assert.Http.Contains(content.Headers, "Content-Type", "application/xml; charset=utf-8");
}
示例7: ComplexTypeSerializesAsOData
private void ComplexTypeSerializesAsOData(string expectedContent, bool json)
{
ObjectContent<Person> content = new ObjectContent<Person>(new Person(0, new ReferenceDepthContext(7)),
_formatter, CollectionTest.GetMediaType(json));
CollectionTest.AssertEqual(json, expectedContent, content.ReadAsStringAsync().Result);
}
示例8: FormatterShouldBeAbleToDeserializeArticle
public async void FormatterShouldBeAbleToDeserializeArticle()
{
var content = new ObjectContent<Article>(_article, _formatter);
var deserializedItem = await content.ReadAsAsync<Article>(new[] { _formatter });
Assert.That(_article, Is.SameAs(deserializedItem));
}
示例9: ContentHeadersAreAddedForJsonMediaType
public void ContentHeadersAreAddedForJsonMediaType()
{
HttpContent content = new ObjectContent<Person[]>(new Person[] { new Person(0, new ReferenceDepthContext(7)) }, _formatter, "application/json");
content.LoadIntoBufferAsync().Wait();
Assert.Http.Contains(content.Headers, "DataServiceVersion", "3.0;");
Assert.Equal(content.Headers.ContentType.MediaType, "application/json");
}
示例10: CanConvertFromStringReturnsFalseForObjectContent
public void CanConvertFromStringReturnsFalseForObjectContent()
{
ObjectContent objectContent = new ObjectContent<int>(5);
HttpParameterValueConverter converter = HttpParameterValueConverter.GetValueConverter(objectContent.GetType());
if (converter.CanConvertFromString)
{
Assert.Fail(string.Format("CanConvertFromString was wrong for ObjectContent."));
}
}
示例11: EntityTypeSerializesAsODataEntry
public void EntityTypeSerializesAsODataEntry()
{
ODataMediaTypeFormatter formatter = CreateFormatter();
Employee employee = (Employee)TypeInitializer.GetInstance(SupportedTypes.Employee);
ObjectContent<Employee> content = new ObjectContent<Employee>(employee, formatter);
RegexReplacement replaceUpdateTime = new RegexReplacement("<updated>*.*</updated>", "<updated>UpdatedTime</updated>");
Assert.Xml.Equal(BaselineResource.TestEntityTypeBasic, content.ReadAsStringAsync().Result, regexReplacements: replaceUpdateTime);
}
示例12: ArrayOfBooleansSerializesAsOData
public void ArrayOfBooleansSerializesAsOData()
{
// Arrange
ObjectContent<bool[]> content = new ObjectContent<bool[]>(new bool[] { true, false, true, false },
_formatter, ODataMediaTypes.ApplicationJsonODataMinimalMetadata);
// Act & Assert
JsonAssert.Equal(Resources.ArrayOfBoolean, content.ReadAsStringAsync().Result);
}
示例13: ArrayOfIntsSerializesAsOData
public void ArrayOfIntsSerializesAsOData()
{
// Arrange
ObjectContent<int[]> content = new ObjectContent<int[]>(new int[] { 10, 20, 30, 40, 50 }, _formatter,
ODataMediaTypes.ApplicationJsonODataMinimalMetadata);
// Act & Assert
JsonAssert.Equal(Resources.ArrayOfInt32, content.ReadAsStringAsync().Result);
}
示例14: ContentHeadersAreAddedForJsonMediaType
public void ContentHeadersAreAddedForJsonMediaType()
{
ODataMediaTypeFormatter formatter = new ODataMediaTypeFormatter(GetSampleModel()) { Request = GetSampleRequest() };
HttpContent content = new ObjectContent<Employee>(new Employee(0, new ReferenceDepthContext(7)), formatter, "application/json");
content.LoadIntoBufferAsync().Wait();
Assert.Http.Contains(content.Headers, "DataServiceVersion", "3.0;");
Assert.Http.Contains(content.Headers, "Content-Type", "application/json; odata=verbose; charset=utf-8");
}
示例15: render_simple_template
public void render_simple_template()
{
var view = new View("Test1", new {Name = "foo"});
var content = new ObjectContent<View>(view, _formatter);
var output = content.ReadAsStringAsync().Result;
Assert.AreEqual("Hello foo! Welcome to Razor!", output);
}