本文整理汇总了C#中System.Net.Http.HttpRequestMessage.SetODataPath方法的典型用法代码示例。如果您正苦于以下问题:C# HttpRequestMessage.SetODataPath方法的具体用法?C# HttpRequestMessage.SetODataPath怎么用?C# HttpRequestMessage.SetODataPath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpRequestMessage
的用法示例。
在下文中一共展示了HttpRequestMessage.SetODataPath方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PostResponse_Throws_IfODataPathDoesNotStartWithEntitySet
public void PostResponse_Throws_IfODataPathDoesNotStartWithEntitySet()
{
ApiController controller = new Mock<ApiController>().Object;
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Customers");
controller.Configuration = new HttpConfiguration();
request.SetODataPath(new ODataPath(new MetadataPathSegment()));
controller.Request = request;
Assert.Throws<InvalidOperationException>(
() => EntitySetControllerHelpers.PostResponse<Customer, int>(controller, new Customer(), 5),
"A Location header could not be generated because the request's OData path does not start with an entity set path segment.");
}
示例2: GetSampleRequest
private HttpRequestMessage GetSampleRequest()
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/employees");
HttpConfiguration configuration = new HttpConfiguration();
string routeName = "Route";
configuration.Routes.MapODataRoute(routeName, null, GetSampleModel());
request.SetConfiguration(configuration);
IEdmEntitySet entitySet = _model.EntityContainers().Single().FindEntitySet("employees");
request.SetEdmModel(_model);
request.SetODataPath(new ODataPath(new EntitySetPathSegment(entitySet)));
request.SetODataRouteName(routeName);
return request;
}
示例3: TryMatchMediaTypeWithBinaryRawValueMatchesRequest
public void TryMatchMediaTypeWithBinaryRawValueMatchesRequest()
{
IEdmModel model = GetBinaryModel();
PropertyAccessPathSegment propertySegment = new PropertyAccessPathSegment((model.GetEdmType(typeof(RawValueEntity)) as IEdmEntityType).FindProperty("BinaryProperty"));
ODataPath path = new ODataPath(new EntitySetPathSegment("RawValue"), new KeyValuePathSegment("1"), propertySegment, new ValuePathSegment());
ODataBinaryValueMediaTypeMapping mapping = new ODataBinaryValueMediaTypeMapping();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/RawValue(1)/BinaryProperty/$value");
request.SetEdmModel(model);
request.SetODataPath(path);
double mapResult = mapping.TryMatchMediaType(request);
Assert.Equal(1.0, mapResult);
}
示例4: TryMatchMediaTypeWithNonRawvalueRequestDoesntMatchRequest
public void TryMatchMediaTypeWithNonRawvalueRequestDoesntMatchRequest()
{
IEdmModel model = ODataTestUtil.GetEdmModel();
PropertyAccessPathSegment propertySegment = new PropertyAccessPathSegment((model.GetEdmType(typeof(FormatterPerson)) as IEdmEntityType).FindProperty("Age"));
ODataPath path = new ODataPath(new EntitySetPathSegment("People"), new KeyValuePathSegment("1"), propertySegment);
ODataPrimitiveValueMediaTypeMapping mapping = new ODataPrimitiveValueMediaTypeMapping();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/People(1)/Age/");
request.SetEdmModel(model);
request.SetODataPath(path);
double mapResult = mapping.TryMatchMediaType(request);
Assert.Equal(0, mapResult);
}
示例5: Match
public override bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection)
{
if (routeDirection != HttpRouteDirection.UriResolution)
{
return true;
}
object odataPathValue;
if (!values.TryGetValue(ODataRouteConstants.ODataPath, out odataPathValue))
{
return false;
}
var pathString = odataPathValue as string;
if (pathString == null) pathString = string.Empty;
ODataPath path = null;
try
{
path = VersionAwarePathHandler.Parse(request, EdmModel, pathString);
}
catch (ODataException e)
{
throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound, "Invalid OData path", e));
}
if (path != null)
{
values[ODataRouteConstants.ODataPath] = path.Segments.First();
// Set all the properties we need for routing, querying, formatting
request.SetEdmModel(EdmModel);
request.SetODataPathHandler(VersionAwarePathHandler);
request.SetODataPath(path);
request.SetODataRouteName(RouteName);
request.SetODataRoutingConventions(RoutingConventions);
if (!values.ContainsKey(ODataRouteConstants.Controller))
{
// Select controller name using the routing conventions
string controllerName = SelectControllerName(path, request);
if (controllerName != null)
{
values[ODataRouteConstants.Controller] = controllerName;
}
}
}
return path != null;
}
示例6: OnActionExecuted_DoesntChangeTheResponse_IfResponseIsntSuccessful
public void OnActionExecuted_DoesntChangeTheResponse_IfResponseIsntSuccessful()
{
IEdmModel model = new Mock<IEdmModel>().Object;
ODataPath path = new ODataPath(new EntitySetPathSegment("FakeEntitySet"),
new KeyValuePathSegment("FakeKey"),
new PropertyAccessPathSegment("FakeProperty"),
new ValuePathSegment());
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/");
request.SetEdmModel(model);
request.SetODataPath(path);
ODataNullValueAttribute odataNullValue = new ODataNullValueAttribute();
HttpResponseMessage response = SetUpResponse(HttpStatusCode.InternalServerError, null, typeof(object));
HttpActionExecutedContext context = SetUpContext(request, response);
odataNullValue.OnActionExecuted(context);
Assert.Equal(response, context.Response);
}
示例7: OnActionExecuted_Generates404_IfContentIsNull
public void OnActionExecuted_Generates404_IfContentIsNull()
{
IEdmModel model = new Mock<IEdmModel>().Object;
ODataPath path = new ODataPath(new EntitySetPathSegment("FakeEntitySet"),
new KeyValuePathSegment("FakeKey"),
new PropertyAccessPathSegment("FakeProperty"),
new ValuePathSegment());
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/");
request.SetEdmModel(model);
request.SetODataPath(path);
ODataNullValueAttribute odataNullValue = new ODataNullValueAttribute();
HttpResponseMessage response = SetUpResponse(HttpStatusCode.OK, null, typeof(object));
HttpActionExecutedContext context = SetUpContext(request, response);
odataNullValue.OnActionExecuted(context);
Assert.NotNull(context.Response);
Assert.Equal(HttpStatusCode.NotFound, context.Response.StatusCode);
}
示例8: Match
/// <summary>
/// Determines whether this instance equals a specified route.
/// </summary>
/// <param name="request">The request.</param>
/// <param name="route">The route to compare.</param>
/// <param name="parameterName">The name of the parameter.</param>
/// <param name="values">A list of parameter values.</param>
/// <param name="routeDirection">The route direction.</param>
/// <returns>
/// True if this instance equals a specified route; otherwise, false.
/// </returns>
public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection)
{
if (request == null)
{
throw Error.ArgumentNull("request");
}
if (values == null)
{
throw Error.ArgumentNull("values");
}
if (routeDirection == HttpRouteDirection.UriResolution)
{
object odataPathRouteValue;
if (values.TryGetValue(ODataRouteConstants.ODataPath, out odataPathRouteValue))
{
string odataPath = odataPathRouteValue as string;
if (odataPath == null)
{
// No odataPath means the path is empty; this is necessary for service documents
odataPath = String.Empty;
}
ODataPath path = _pathHandler.Parse(odataPath);
if (path != null)
{
request.SetODataPath(path);
return true;
}
}
return false;
}
else
{
// This constraint only applies to URI resolution
return true;
}
}
示例9: AddRequestInfo
private void AddRequestInfo(HttpRequestMessage request)
{
request.SetODataPath(new DefaultODataPathHandler().Parse(_model, GetODataPath(
request.RequestUri.AbsoluteUri)));
request.SetEdmModel(_model);
request.SetFakeODataRouteName();
}
示例10: Property_LocationHeader_IsEvaluatedOnlyOnce
public void Property_LocationHeader_IsEvaluatedOnlyOnce()
{
// Arrange
Uri editLink = new Uri("http://edit-link");
Mock<EntitySetLinkBuilderAnnotation> linkBuilder = new Mock<EntitySetLinkBuilderAnnotation>();
linkBuilder.Setup(b => b.BuildEditLink(It.IsAny<EntityInstanceContext>(), ODataMetadataLevel.Default, null))
.Returns(editLink);
CustomersModelWithInheritance model = new CustomersModelWithInheritance();
model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(TestEntity)));
model.Model.SetEntitySetLinkBuilder(model.Customers, linkBuilder.Object);
ODataPath path = new ODataPath(new EntitySetPathSegment(model.Customers));
HttpRequestMessage request = new HttpRequestMessage();
request.SetEdmModel(model.Model);
request.SetODataPath(path);
TestController controller = new TestController { Request = request, Configuration = new HttpConfiguration() };
CreatedODataResult<TestEntity> createdODataResult = new CreatedODataResult<TestEntity>(_entity, controller);
// Act
Uri locationHeader = createdODataResult.LocationHeader;
// Assert
linkBuilder.Verify(
(b) => b.BuildEditLink(It.IsAny<EntityInstanceContext>(), ODataMetadataLevel.Default, null),
Times.Once());
}
示例11: GetODataPayloadSerializer_ReturnsRawValueSerializer_ForValueRequests
public void GetODataPayloadSerializer_ReturnsRawValueSerializer_ForValueRequests(Type type, EdmPrimitiveTypeKind edmPrimitiveTypeKind)
{
ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider();
HttpRequestMessage request = new HttpRequestMessage();
request.SetODataPath(new ODataPath(new ValuePathSegment()));
var serializer = serializerProvider.GetODataPayloadSerializer(_edmModel, type, request);
Assert.NotNull(serializer);
Assert.Equal(ODataPayloadKind.Value, serializer.ODataPayloadKind);
}
示例12: GenerateLocationHeader_UsesEntitySetLinkBuilder_ToGenerateLocationHeader
public void GenerateLocationHeader_UsesEntitySetLinkBuilder_ToGenerateLocationHeader()
{
// Arrange
string idLink = "http://id-link";
Uri editLink = new Uri(idLink);
Mock<EntitySetLinkBuilderAnnotation> linkBuilder = new Mock<EntitySetLinkBuilderAnnotation>();
linkBuilder.Setup(b => b.BuildIdLink(It.IsAny<EntityInstanceContext>(), ODataMetadataLevel.Default))
.Returns(idLink);
linkBuilder.Setup(b => b.BuildEditLink(It.IsAny<EntityInstanceContext>(), ODataMetadataLevel.Default, idLink))
.Returns(editLink);
CustomersModelWithInheritance model = new CustomersModelWithInheritance();
model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(TestEntity)));
model.Model.SetEntitySetLinkBuilder(model.Customers, linkBuilder.Object);
ODataPath path = new ODataPath(new EntitySetPathSegment(model.Customers));
HttpRequestMessage request = new HttpRequestMessage();
request.SetEdmModel(model.Model);
request.SetODataPath(path);
CreatedODataResult<TestEntity> createdODataResult = GetCreatedODataResult(request);
// Act
var locationHeader = createdODataResult.GenerateLocationHeader();
// Assert
Assert.Same(editLink, locationHeader);
}
示例13: GenerateLocationHeader_ThrowsEditLinkNullForLocationHeader_IfEntitySetLinkBuilderReturnsNull
public void GenerateLocationHeader_ThrowsEditLinkNullForLocationHeader_IfEntitySetLinkBuilderReturnsNull()
{
// Arrange
Mock<EntitySetLinkBuilderAnnotation> linkBuilder = new Mock<EntitySetLinkBuilderAnnotation>();
CustomersModelWithInheritance model = new CustomersModelWithInheritance();
model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(TestEntity)));
model.Model.SetEntitySetLinkBuilder(model.Customers, linkBuilder.Object);
ODataPath path = new ODataPath(new EntitySetPathSegment(model.Customers));
HttpRequestMessage request = new HttpRequestMessage();
request.SetEdmModel(model.Model);
request.SetODataPath(path);
CreatedODataResult<TestEntity> createdODataResult = GetCreatedODataResult(request);
// Act
Assert.Throws<InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(),
"The edit link builder for the entity set 'Customers' returned null. An edit link is required for the location header.");
}
示例14: GenerateLocationHeader_ThrowsEntityTypeNotInModel_IfContentTypeIsNotThereInModel
public void GenerateLocationHeader_ThrowsEntityTypeNotInModel_IfContentTypeIsNotThereInModel()
{
CustomersModelWithInheritance model = new CustomersModelWithInheritance();
ODataPath path = new ODataPath(new EntitySetPathSegment(model.Customers));
HttpRequestMessage request = new HttpRequestMessage();
request.SetEdmModel(model.Model);
request.SetODataPath(path);
CreatedODataResult<TestEntity> createdODataResult = GetCreatedODataResult(request);
// Act & Assert
Assert.Throws<InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(),
"Cannot find the entity type 'System.Web.Http.OData.Results.CreatedODataResultTest+TestEntity' in the model.");
}
示例15: GenerateLocationHeader_ThrowsTypeMustBeEntity_IfMappingTypeIsNotEntity
public void GenerateLocationHeader_ThrowsTypeMustBeEntity_IfMappingTypeIsNotEntity()
{
CustomersModelWithInheritance model = new CustomersModelWithInheritance();
ODataPath path = new ODataPath(new EntitySetPathSegment(model.Customers));
HttpRequestMessage request = new HttpRequestMessage();
request.SetEdmModel(model.Model);
request.SetODataPath(path);
model.Model.SetAnnotationValue(model.Address, new ClrTypeAnnotation(typeof(TestEntity)));
CreatedODataResult<TestEntity> createdODataResult = GetCreatedODataResult(request);
// Act & Assert
Assert.Throws<InvalidOperationException>(() => createdODataResult.GenerateLocationHeader(),
"NS.Address is not an entity type. Only entity types are supported.");
}