本文整理汇总了C#中ObjectResult类的典型用法代码示例。如果您正苦于以下问题:C# ObjectResult类的具体用法?C# ObjectResult怎么用?C# ObjectResult使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ObjectResult类属于命名空间,在下文中一共展示了ObjectResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OnActionExecuted
public void OnActionExecuted([NotNull] ActionExecutedContext context)
{
var httpResponseException = context.Exception as HttpResponseException;
if (httpResponseException != null)
{
var request = context.HttpContext.GetHttpRequestMessage();
var response = httpResponseException.Response;
if (response != null && response.RequestMessage == null)
{
response.RequestMessage = request;
}
var objectResult = new ObjectResult(response)
{
DeclaredType = typeof(HttpResponseMessage)
};
context.Result = objectResult;
// Its marked as handled as in webapi because an HttpResponseException
// was considered as a 'success' response.
context.ExceptionHandled = true;
}
}
示例2: 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(),
};
// 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);
}
示例3: GameAndAreaAndRoomResultsFromDb
public GameData GameAndAreaAndRoomResultsFromDb(ObjectResult<ReadGameStateForPlayer_Result> gameStateResults)
{
GameState gameState = null;
AreaData areaData = null;
gameState = gameStateResults.Select(x => new GameState() { LastTime = x.LastTime, StopTime = x.StopTime }).SingleOrDefault();
var areaResults = gameStateResults.GetNextResult<ReadArea_Result>();
areaData = AreaAndRoomResultsFromDb(areaResults);
return new GameData(gameState, areaData.Item1, areaData.Item2, areaData.Item3, areaData.Item4);
}
示例4: AreaAndRoomResultsFromDb
public AreaData AreaAndRoomResultsFromDb(ObjectResult<ReadArea_Result> areaResults)
{
Area area = null;
RoomData roomData = null;
area = TypeAdapter.Adapt<Area>(areaResults.Single());
var roomResults = areaResults.GetNextResult<ReadRoom_Result>();
roomData = RoomResultsFromDB(roomResults);
return new AreaData(area, roomData.Item1, roomData.Item2, roomData.Item3);
}
示例5: ExecuteAsync_ContentTypeProvidedFromResponseAndObjectResult_UsesResponseContentType
public async Task ExecuteAsync_ContentTypeProvidedFromResponseAndObjectResult_UsesResponseContentType()
{
// Arrange
var executor = CreateCustomObjectResultExecutor();
var httpContext = new DefaultHttpContext();
var actionContext = new ActionContext() { HttpContext = httpContext };
httpContext.Request.Headers[HeaderNames.Accept] = "application/xml"; // This will not be used
httpContext.Response.ContentType = "text/plain";
var result = new ObjectResult("input");
result.Formatters.Add(new TestXmlOutputFormatter());
result.Formatters.Add(new TestJsonOutputFormatter());
result.Formatters.Add(new TestStringOutputFormatter()); // This will be chosen based on the content type
// Act
await executor.ExecuteAsync(actionContext, result);
// Assert
Assert.IsType<TestStringOutputFormatter>(executor.SelectedOutputFormatter);
MediaTypeAssert.Equal("text/plain; charset=utf-8", httpContext.Response.ContentType);
}
示例6: 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);
}
示例7: 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>()));
}
示例8: 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);
}
示例9: 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);
}
示例10: 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);
}
示例11: 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);
}
示例12: 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);
}
示例13: parseObjectListWithMetadata
private List<ObjectResult> parseObjectListWithMetadata(string responseStr)
{
List<ObjectResult> objs = new List<ObjectResult>();
try
{
XmlDocument d = new XmlDocument();
d.LoadXml(responseStr);
XmlNodeList olist = d.GetElementsByTagName("Object");
log.TraceEvent(TraceEventType.Verbose, 0, "Found " + olist.Count + " results");
foreach (XmlNode xn in olist)
{
ObjectResult obj = new ObjectResult();
// Get the objectId
obj.Id = new ObjectId(getChildByName("ObjectID", xn.ChildNodes).InnerText);
// Next, get the metadata
obj.meta = new MetadataList();
XmlNode sMetaNode = getChildByName("SystemMetadataList", xn.ChildNodes);
XmlNode uMetaNode = getChildByName("UserMetadataList", xn.ChildNodes);
if (sMetaNode != null)
{
foreach (XmlNode metaNode in sMetaNode.ChildNodes)
{
if (!metaNode.LocalName.Equals("Metadata"))
{
continue;
}
string mName = getChildByName("Name", metaNode.ChildNodes).InnerText;
string mValue = getChildByName("Value", metaNode.ChildNodes).InnerText;
obj.meta.AddMetadata(new Metadata(mName, mValue, false));
}
}
if (uMetaNode != null)
{
foreach (XmlNode metaNode in uMetaNode.ChildNodes)
{
if (!metaNode.LocalName.Equals("Metadata"))
{
continue;
}
string mName = getChildByName("Name", metaNode.ChildNodes).InnerText;
string mValue = getChildByName("Value", metaNode.ChildNodes).InnerText;
string mListable = getChildByName("Listable", metaNode.ChildNodes).InnerText;
obj.meta.AddMetadata(new Metadata(mName, mValue, "true".Equals(mListable)));
}
}
objs.Add(obj);
}
}
catch (XmlException e)
{
throw new EsuException("Error parsing xml object list", e);
}
return objs;
}
示例14: RoomResultsFromDB
public RoomData RoomResultsFromDB(ObjectResult<ReadRoom_Result> roomResults)
{
Room room = null;
IEnumerable<RoomState> roomStates = null;
IEnumerable<ParagraphState> paragraphStates = null;
room = roomResults.Select(x => TypeAdapter.Adapt<Room>(x)).FirstOrDefault();
var roomStateResults = roomResults.GetNextResult<ReadRoomStatesForPlayerRoom_Result>();
roomStates = roomStateResults.Select(x => TypeAdapter.Adapt<RoomState>(x)).ToList();
var paragraphStateResults = roomStateResults.GetNextResult<ReadParagraphStatesForPlayerRoom_Result>();
paragraphStates = paragraphStateResults.Select(x => TypeAdapter.Adapt<ParagraphState>(x)).ToList();
var nounResults = paragraphStateResults.GetNextResult<ReadNounsForPlayerRoom_Result>();
IEnumerable<Noun> nouns = nounResults.Select(x => TypeAdapter.Adapt<Noun>(x)).ToList();
foreach (var paragraphState in paragraphStates)
paragraphState.Nouns = nouns.Where(x => x.ParagraphState == paragraphState.Id).ToArray();
return new RoomData(room, roomStates, paragraphStates);
}
示例15: 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);
}