本文整理汇总了C#中ObjectResult.ExecuteResultAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectResult.ExecuteResultAsync方法的具体用法?C# ObjectResult.ExecuteResultAsync怎么用?C# ObjectResult.ExecuteResultAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectResult
的用法示例。
在下文中一共展示了ObjectResult.ExecuteResultAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ObjectResult_WithMultipleContentTypesAndAcceptHeaders_PerformsContentNegotiation
public async Task ObjectResult_WithMultipleContentTypesAndAcceptHeaders_PerformsContentNegotiation(
IEnumerable<string> contentTypes, string acceptHeader, string expectedHeader)
{
// Arrange
var expectedContentType = expectedHeader;
var input = "testInput";
var stream = new MemoryStream();
var httpResponse = new Mock<HttpResponse>();
var tempContentType = string.Empty;
httpResponse.SetupProperty<string>(o => o.ContentType);
httpResponse.SetupGet(r => r.Body).Returns(stream);
var actionContext = CreateMockActionContext(httpResponse.Object, acceptHeader);
var result = new ObjectResult(input);
// Set the content type property explicitly.
result.ContentTypes = contentTypes.Select(contentType => MediaTypeHeaderValue.Parse(contentType)).ToList();
result.Formatters = new List<IOutputFormatter>
{
new CannotWriteFormatter(),
new JsonOutputFormatter(JsonOutputFormatter.CreateDefaultSettings(), false),
};
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
// should always select the Json Output formatter even though it is second in the list.
httpResponse.VerifySet(r => r.ContentType = expectedContentType);
}
示例2: ObjectResult_MatchAllContentType_Throws
public async Task ObjectResult_MatchAllContentType_Throws(string content, string invalidContentType)
{
// Arrange
var contentTypes = content.Split(',');
var objectResult = new ObjectResult(new Person() { Name = "John" });
objectResult.ContentTypes = contentTypes.Select(contentType => MediaTypeHeaderValue.Parse(contentType))
.ToList();
var actionContext = CreateMockActionContext();
// Act & Assert
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
() => objectResult.ExecuteResultAsync(actionContext));
var expectedMessage = string.Format("The content-type '{0}' added in the 'ContentTypes' property is " +
"invalid. Media types which match all types or match all subtypes are not supported.",
invalidContentType);
Assert.Equal(expectedMessage, exception.Message);
}
示例3: ObjectResult_WildcardAcceptMediaType_AndExplicitResponseContentType
public async Task ObjectResult_WildcardAcceptMediaType_AndExplicitResponseContentType(
string acceptHeader,
string expectedResponseContentType,
bool respectBrowserAcceptHeader)
{
// Arrange
var objectResult = new ObjectResult(new Person() { Name = "John" });
objectResult.ContentTypes.Add(MediaTypeHeaderValue.Parse("application/xml"));
objectResult.ContentTypes.Add(MediaTypeHeaderValue.Parse("application/json"));
var outputFormatters = new IOutputFormatter[] {
new HttpNoContentOutputFormatter(),
new StringOutputFormatter(),
new JsonOutputFormatter(),
new XmlDataContractSerializerOutputFormatter()
};
var response = GetMockHttpResponse();
var actionContext = CreateMockActionContext(
outputFormatters,
response.Object,
acceptHeader,
respectBrowserAcceptHeader: respectBrowserAcceptHeader);
// Act
await objectResult.ExecuteResultAsync(actionContext);
// Assert
response.VerifySet(resp => resp.ContentType = expectedResponseContentType);
}
示例4: ObjectResult_PerformsContentNegotiation_OnAllMediaRangeAcceptHeaderMediaType
public async Task ObjectResult_PerformsContentNegotiation_OnAllMediaRangeAcceptHeaderMediaType(
string acceptHeader,
string expectedResponseContentType)
{
// Arrange
var objectResult = new ObjectResult(new Person() { Name = "John" });
var outputFormatters = new IOutputFormatter[] {
new HttpNoContentOutputFormatter(),
new StringOutputFormatter(),
new JsonOutputFormatter(),
new XmlDataContractSerializerOutputFormatter()
};
var response = GetMockHttpResponse();
var actionContext = CreateMockActionContext(
outputFormatters,
response.Object,
requestAcceptHeader: acceptHeader,
respectBrowserAcceptHeader: true);
// Act
await objectResult.ExecuteResultAsync(actionContext);
// Assert
response.VerifySet(resp => resp.ContentType = expectedResponseContentType);
}
示例5: ObjectResult_Execute_CallsJsonResult_SetsContent
public async Task ObjectResult_Execute_CallsJsonResult_SetsContent()
{
// Arrange
var expectedContentType = "application/json; charset=utf-8";
var nonStringValue = new { x1 = 10, y1 = "Hello" };
var httpResponse = Mock.Of<HttpResponse>();
httpResponse.Body = new MemoryStream();
var actionContext = CreateMockActionContext(httpResponse);
var tempStream = new MemoryStream();
var tempHttpContext = new Mock<HttpContext>();
var tempHttpResponse = new Mock<HttpResponse>();
tempHttpResponse.SetupGet(o => o.Body).Returns(tempStream);
tempHttpResponse.SetupProperty<string>(o => o.ContentType);
tempHttpContext.SetupGet(o => o.Request).Returns(new DefaultHttpContext().Request);
tempHttpContext.SetupGet(o => o.Response).Returns(tempHttpResponse.Object);
var tempActionContext = new ActionContext(tempHttpContext.Object,
new RouteData(),
new ActionDescriptor());
var formatterContext = new OutputFormatterContext()
{
HttpContext = tempActionContext.HttpContext,
Object = nonStringValue,
DeclaredType = nonStringValue.GetType()
};
var formatter = new JsonOutputFormatter();
formatter.WriteResponseHeaders(formatterContext);
await formatter.WriteAsync(formatterContext);
// Act
var result = new ObjectResult(nonStringValue);
await result.ExecuteResultAsync(actionContext);
// Assert
Assert.Equal(expectedContentType, httpResponse.ContentType);
Assert.Equal(tempStream.ToArray(), ((MemoryStream)actionContext.HttpContext.Response.Body).ToArray());
}
示例6: ObjectResult_Execute_NullContent_SetsStatusCode
public async Task ObjectResult_Execute_NullContent_SetsStatusCode()
{
// Arrange
var stream = new MemoryStream();
var expectedStatusCode = StatusCodes.Status201Created;
var httpResponse = new Mock<HttpResponse>();
httpResponse.SetupGet(r => r.Body).Returns(stream);
var formatters = new IOutputFormatter[]
{
new HttpNoContentOutputFormatter(),
new StringOutputFormatter(),
new JsonOutputFormatter()
};
var actionContext = CreateMockActionContext(formatters,
httpResponse.Object,
requestAcceptHeader: null,
requestContentType: null);
var result = new ObjectResult(null);
result.StatusCode = expectedStatusCode;
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
httpResponse.VerifySet(r => r.StatusCode = expectedStatusCode);
Assert.Equal(0, httpResponse.Object.Body.Length);
}
示例7: NoAcceptAndContentTypeHeaders_406Formatter_DoesNotTakeEffect
public async Task NoAcceptAndContentTypeHeaders_406Formatter_DoesNotTakeEffect()
{
// Arrange
var expectedContentType = "application/json; charset=utf-8";
var input = 123;
var httpResponse = new DefaultHttpContext().Response;
httpResponse.Body = new MemoryStream();
var actionContext = CreateMockActionContext(
outputFormatters: new IOutputFormatter[]
{
new HttpNotAcceptableOutputFormatter(),
new JsonOutputFormatter()
},
response: httpResponse,
requestAcceptHeader: null,
requestContentType: null,
requestAcceptCharsetHeader: null);
var result = new ObjectResult(input);
result.ContentTypes = new List<MediaTypeHeaderValue>();
result.ContentTypes.Add(MediaTypeHeaderValue.Parse(expectedContentType));
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
Assert.Equal(expectedContentType, httpResponse.ContentType);
}
示例8: ObjectResult_NoFormatterFound_Returns406
public async Task ObjectResult_NoFormatterFound_Returns406()
{
// Arrange
var stream = new MemoryStream();
var httpResponse = new Mock<HttpResponse>();
httpResponse.SetupProperty<string>(o => o.ContentType);
httpResponse.SetupGet(r => r.Body).Returns(stream);
var actionContext = CreateMockActionContext(httpResponse.Object,
requestAcceptHeader: null,
requestContentType: null);
var input = "testInput";
var result = new ObjectResult(input);
// Set more than one formatters. The test output formatter throws on write.
result.Formatters = new List<IOutputFormatter>
{
new CannotWriteFormatter(),
};
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
// Asserts that content type is not text/custom.
httpResponse.VerifySet(r => r.StatusCode = StatusCodes.Status406NotAcceptable);
}
示例9: MemoryStream
ObjectResult_NoContentTypeSetWithNoAcceptHeadersAndNoRequestContentType_PicksFirstFormatterWhichCanWrite()
{
// Arrange
var stream = new MemoryStream();
var expectedContentType = "application/json; charset=utf-8";
var httpResponse = new Mock<HttpResponse>();
httpResponse.SetupProperty<string>(o => o.ContentType);
httpResponse.SetupGet(r => r.Body).Returns(stream);
var actionContext = CreateMockActionContext(httpResponse.Object,
requestAcceptHeader: null,
requestContentType: null);
var input = "testInput";
var result = new ObjectResult(input);
// Set more than one formatters. The test output formatter throws on write.
result.Formatters = new List<IOutputFormatter>
{
new CannotWriteFormatter(),
new JsonOutputFormatter(),
};
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
// Asserts that content type is not text/custom.
httpResponse.VerifySet(r => r.ContentType = expectedContentType);
}
示例10: ObjectResult_NoContentTypeSetWithAcceptHeaders_PicksFormatterOnAcceptHeaders
public async Task ObjectResult_NoContentTypeSetWithAcceptHeaders_PicksFormatterOnAcceptHeaders()
{
// Arrange
var expectedContentType = "application/json; charset=utf-8";
var input = "testInput";
var stream = new MemoryStream();
var httpResponse = GetMockHttpResponse();
var actionContext =
CreateMockActionContext(httpResponse.Object,
requestAcceptHeader: "text/custom;q=0.1,application/json;q=0.9",
requestContentType: "application/custom");
var result = new ObjectResult(input);
// Set more than one formatters. The test output formatter throws on write.
result.Formatters = new List<IOutputFormatter>
{
new CannotWriteFormatter(),
new JsonOutputFormatter(),
};
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
// Asserts that content type is not text/custom. i.e the formatter is not TestOutputFormatter.
httpResponse.VerifySet(r => r.ContentType = expectedContentType);
}
示例11: ObjectResult_MultipleFormattersSupportingTheSameContentType_SelectsTheFirstFormatterInList
public async Task ObjectResult_MultipleFormattersSupportingTheSameContentType_SelectsTheFirstFormatterInList()
{
// Arrange
var input = "testInput";
var stream = new MemoryStream();
var httpResponse = GetMockHttpResponse();
var actionContext = CreateMockActionContext(httpResponse.Object, requestAcceptHeader: null);
var result = new ObjectResult(input);
// It should select the mock formatter as that is the first one in the list.
var contentTypes = new[] { "application/json", "text/custom" };
var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse("text/custom");
// Get a mock formatter which supports everything.
var mockFormatter = GetMockFormatter();
result.ContentTypes = contentTypes.Select(contentType => MediaTypeHeaderValue.Parse(contentType)).ToList();
result.Formatters = new List<IOutputFormatter>
{
mockFormatter.Object,
new JsonOutputFormatter(),
new CannotWriteFormatter()
};
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
// Verify that mock formatter was chosen.
mockFormatter.Verify(o => o.WriteAsync(It.IsAny<OutputFormatterContext>()));
}
示例12: ObjectResult_MultipleContentTypes_PicksFirstFormatterWhichSupportsAnyOfTheContentTypes
public async Task ObjectResult_MultipleContentTypes_PicksFirstFormatterWhichSupportsAnyOfTheContentTypes()
{
// Arrange
var expectedContentType = "application/json; charset=utf-8";
var input = "testInput";
var httpResponse = GetMockHttpResponse();
var actionContext = CreateMockActionContext(httpResponse.Object, requestAcceptHeader: null);
var result = new ObjectResult(input);
// It should not select TestOutputFormatter,
// This is because it should accept the first formatter which supports any of the two contentTypes.
var contentTypes = new[] { "application/custom", "application/json" };
// Set the content type and the formatters property explicitly.
result.ContentTypes = contentTypes.Select(contentType => MediaTypeHeaderValue.Parse(contentType))
.ToList();
result.Formatters = new List<IOutputFormatter>
{
new CannotWriteFormatter(),
new JsonOutputFormatter(),
};
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
// Asserts that content type is not text/custom.
httpResponse.VerifySet(r => r.ContentType = expectedContentType);
}
示例13: ObjectResult_WithSingleContentType_TheContentTypeIsIgnoredIfTheTypeIsString
public async Task ObjectResult_WithSingleContentType_TheContentTypeIsIgnoredIfTheTypeIsString()
{
// Arrange
var contentType = "application/json;charset=utf-8";
var expectedContentType = "text/plain; charset=utf-8";
// string value.
var input = "1234";
var httpResponse = GetMockHttpResponse();
var actionContext = CreateMockActionContext(httpResponse.Object);
// Set the content type property explicitly to a single value.
var result = new ObjectResult(input);
result.ContentTypes = new List<MediaTypeHeaderValue>();
result.ContentTypes.Add(MediaTypeHeaderValue.Parse(contentType));
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
httpResponse.VerifySet(r => r.ContentType = expectedContentType);
}
示例14: ObjectResult_FallsBackOn_FormattersInOptions
public async Task ObjectResult_FallsBackOn_FormattersInOptions()
{
// Arrange
var formatter = GetMockFormatter();
var actionContext = CreateMockActionContext(
new[] { formatter.Object },
setupActionBindingContext: false);
// Set the content type property explicitly to a single value.
var result = new ObjectResult("someValue");
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
formatter.Verify(o => o.WriteAsync(It.IsAny<OutputFormatterContext>()));
}
示例15: ObjectResult_WithSingleContentType_TheGivenContentTypeIsSelected
public async Task ObjectResult_WithSingleContentType_TheGivenContentTypeIsSelected()
{
// Arrange
var expectedContentType = "application/json; charset=utf-8";
// non string value.
var input = 123;
var httpResponse = new DefaultHttpContext().Response;
httpResponse.Body = new MemoryStream();
var actionContext = CreateMockActionContext(httpResponse);
// Set the content type property explicitly to a single value.
var result = new ObjectResult(input);
result.ContentTypes = new List<MediaTypeHeaderValue>();
result.ContentTypes.Add(MediaTypeHeaderValue.Parse(expectedContentType));
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
Assert.Equal(expectedContentType, httpResponse.ContentType);
}