本文整理汇总了C#中System.Net.Http.HttpRequestMessage.ODataProperties方法的典型用法代码示例。如果您正苦于以下问题:C# HttpRequestMessage.ODataProperties方法的具体用法?C# HttpRequestMessage.ODataProperties怎么用?C# HttpRequestMessage.ODataProperties使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpRequestMessage
的用法示例。
在下文中一共展示了HttpRequestMessage.ODataProperties方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetVersion
private ODataVersion? GetVersion(HttpRequestMessage request)
{
// The logic is as follows. We check DataServiceVersion first and if not present we check MaxDataServiceVersion.
// In order to consider any header valid, it needs to have exactly one valid value, for example a header with
// value 3.0 will be considered valid, but a header with two values 3.0, 4.0 won't. The user experience is
// better if you fail with a 404 here than if you "guess" the version. The moment we see an invalid header
// we return a null version. The spec says that in the case of an invalid odata version the service should
// return 4xx.
int versionHeaderCount = GetHeaderCount(HttpRequestMessageProperties.ODataServiceVersionHeader, request);
int maxVersionHeaderCount = GetHeaderCount(HttpRequestMessageProperties.ODataMaxServiceVersionHeader, request);
if ((versionHeaderCount == 1 && request.ODataProperties().ODataServiceVersion != null))
{
return request.ODataProperties().ODataServiceVersion;
}
else if ((versionHeaderCount == 0 && maxVersionHeaderCount == 1 &&
request.ODataProperties().ODataMaxServiceVersion != null))
{
return request.ODataProperties().ODataMaxServiceVersion;
}
else if (versionHeaderCount == 0 && maxVersionHeaderCount == 0)
{
return Version;
}
else
{
return null;
}
}
示例2: CreateQueryOptions_SetsContextProperties_WithModelAndPath
public void CreateQueryOptions_SetsContextProperties_WithModelAndPath()
{
// Arrange
ApiController controller = new Mock<ApiController>().Object;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customers");
controller.Configuration = new HttpConfiguration();
ODataModelBuilder odataModel = new ODataModelBuilder();
string setName = typeof(Customer).Name;
odataModel.EntityType<Customer>().HasKey(c => c.Id);
odataModel.EntitySet<Customer>(setName);
IEdmModel model = odataModel.GetEdmModel();
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet(setName);
request.ODataProperties().Model = model;
request.ODataProperties().Path = new ODataPath(new EntitySetPathSegment(entitySet));
controller.Request = request;
// Act
ODataQueryOptions<Customer> queryOptions =
EntitySetControllerHelpers.CreateQueryOptions<Customer>(controller);
// Assert
Assert.Same(model, queryOptions.Context.Model);
Assert.Same(entitySet, queryOptions.Context.NavigationSource);
Assert.Same(typeof(Customer), queryOptions.Context.ElementClrType);
}
示例3: PathHandlerGetter_ReturnsDefaultPathHandlerByDefault
public void PathHandlerGetter_ReturnsDefaultPathHandlerByDefault()
{
HttpRequestMessage request = new HttpRequestMessage();
IEdmModel model = new EdmModel();
request.ODataProperties().Model = model;
var pathHandler = request.ODataProperties().PathHandler;
Assert.NotNull(pathHandler);
Assert.IsType<DefaultODataPathHandler>(pathHandler);
}
示例4: PathHandlerGetter_Returns_PathHandlerSetter
public void PathHandlerGetter_Returns_PathHandlerSetter()
{
HttpRequestMessage request = new HttpRequestMessage();
IODataPathHandler parser = new Mock<IODataPathHandler>().Object;
// Act
request.ODataProperties().PathHandler = parser;
// Assert
Assert.Same(parser, request.ODataProperties().PathHandler);
}
示例5: SendAsync
/// <inheritdoc/>
protected async override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (request == null)
{
throw Error.ArgumentNull("request");
}
HttpConfiguration configuration = request.GetConfiguration();
if (configuration == null)
{
throw Error.InvalidOperation(SRResources.RequestMustContainConfiguration);
}
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
// Do not interfere with null responses, we want to buble it up to the top.
// Do not handle 204 responses as the spec says a 204 response must not include an ETag header
// unless the request's representation data was saved without any transformation applied to the body
// (i.e., the resource's new representation data is identical to the representation data received in the
// PUT request) and the ETag value reflects the new representation.
// Even in that case returning an ETag is optional and it requires access to the original object which is
// not possible with the current architecture, so if the user is interested he can set the ETag in that
// case by himself on the response.
if (response == null || !response.IsSuccessStatusCode || response.StatusCode == HttpStatusCode.NoContent)
{
return response;
}
ODataPath path = request.ODataProperties().Path;
IEdmModel model = request.ODataProperties().Model;
IEdmEntityType edmType = GetSingleEntityEntityType(path);
object value = GetSingleEntityObject(response);
IEdmEntityTypeReference typeReference = GetTypeReference(model, edmType, value);
if (typeReference != null)
{
EntityInstanceContext context = CreateInstanceContext(typeReference, value);
context.EdmModel = model;
context.NavigationSource = path.NavigationSource;
IETagHandler etagHandler = configuration.GetETagHandler();
EntityTagHeaderValue etag = CreateETag(context, etagHandler);
if (etag != null)
{
response.Headers.ETag = etag;
}
}
return response;
}
示例6: GetRequest
private HttpRequestMessage GetRequest(IEdmModel model, IEdmSingleton singleton)
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapODataServiceRoute("odata", "odata", model);
HttpRequestMessage request = new HttpRequestMessage();
request.SetConfiguration(config);
request.ODataProperties().PathHandler = new DefaultODataPathHandler();
request.ODataProperties().RouteName = "odata";
request.ODataProperties().Model = model;
request.ODataProperties().Path = new ODataPath(new[] { new SingletonPathSegment(singleton) });
request.RequestUri = new Uri("http://localhost/odata/Boss");
return request;
}
示例7: ModelGetter_Returns_ModelSetter
public void ModelGetter_Returns_ModelSetter()
{
// Arrange
HttpRequestMessage request = new HttpRequestMessage();
IEdmModel model = new EdmModel();
// Act
request.ODataProperties().Model = model;
IEdmModel newModel = request.ODataProperties().Model;
// Assert
Assert.Same(model, newModel);
}
示例8: GetSampleRequest
private HttpRequestMessage GetSampleRequest()
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/employees");
request.ODataProperties().Model = _model;
HttpConfiguration configuration = new HttpConfiguration();
string routeName = "Route";
configuration.Routes.MapODataServiceRoute(routeName, null, _model);
request.SetConfiguration(configuration);
IEdmEntitySet entitySet = _model.EntityContainers().Single().FindEntitySet("employees");
request.ODataProperties().Path = new ODataPath(new EntitySetPathSegment(entitySet));
request.ODataProperties().RouteName = routeName;
return request;
}
示例9: TryMatchMediaType_WithNonRawvalueRequest_DoesntMatchRequest
public void TryMatchMediaType_WithNonRawvalueRequest_DoesntMatchRequest()
{
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.ODataProperties().Model = model;
request.ODataProperties().Path = path;
double mapResult = mapping.TryMatchMediaType(request);
Assert.Equal(0, mapResult);
}
示例10: 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.ODataProperties().Model = model;
request.ODataProperties().Path = path;
double mapResult = mapping.TryMatchMediaType(request);
Assert.Equal(1.0, mapResult);
}
示例11: NavigationLinksGenerationConvention_GeneratesLinksWithCast_ForDerivedProperties
public void NavigationLinksGenerationConvention_GeneratesLinksWithCast_ForDerivedProperties()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Vehicle>("vehicles");
builder.EntitySet<Manufacturer>("manufacturers");
IEdmModel model = builder.GetEdmModel();
IEdmEntitySet vehiclesEdmEntitySet = model.EntityContainer.FindEntitySet("vehicles");
IEdmEntityType carType = model.AssertHasEntityType(typeof(Car));
IEdmNavigationProperty carManufacturerProperty = carType.AssertHasNavigationProperty(model, "Manufacturer", typeof(CarManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);
HttpConfiguration configuration = new HttpConfiguration();
string routeName = "Route";
configuration.Routes.MapODataServiceRoute(routeName, null, model);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
request.SetConfiguration(configuration);
request.ODataProperties().RouteName = routeName;
NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehiclesEdmEntitySet);
var serializerContext = new ODataSerializerContext { Model = model, NavigationSource = vehiclesEdmEntitySet, Url = request.GetUrlHelper() };
var entityContext = new EntityInstanceContext(serializerContext, carType.AsReference(), new Car { Model = 2009, Name = "Accord" });
Uri uri = linkBuilder.BuildNavigationLink(entityContext, carManufacturerProperty, ODataMetadataLevel.Default);
Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/System.Web.OData.Builder.TestModels.Car/Manufacturer", uri.AbsoluteUri);
}
示例12: Convention_GeneratesUri_ForActionBoundToEntity
public void Convention_GeneratesUri_ForActionBoundToEntity()
{
// Arrange
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Customer>("Customers");
var action = builder.EntityType<Customer>().Action("MyAction");
action.Parameter<string>("param");
IEdmModel model = builder.GetEdmModel();
// Act
HttpConfiguration configuration = new HttpConfiguration();
configuration.MapODataServiceRoute("odata", "odata", model);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:123");
request.SetConfiguration(configuration);
request.ODataProperties().RouteName = "odata";
IEdmEntitySet customers = model.EntityContainer.FindEntitySet("Customers");
var edmType = model.SchemaElements.OfType<IEdmEntityType>().First(e => e.Name == "Customer");
var serializerContext = new ODataSerializerContext { Model = model, NavigationSource = customers, Url = request.GetUrlHelper() };
var entityContext = new EntityInstanceContext(serializerContext, edmType.AsReference(), new Customer { Id = 109 });
// Assert
var edmAction = model.SchemaElements.OfType<IEdmAction>().First(f => f.Name == "MyAction");
Assert.NotNull(edmAction);
ActionLinkBuilder actionLinkBuilder = model.GetActionLinkBuilder(edmAction);
Uri link = actionLinkBuilder.BuildActionLink(entityContext);
Assert.Equal("http://localhost:123/odata/Customers(109)/Default.MyAction", link.AbsoluteUri);
}
示例13: SendAsync
/// <inheritdoc />
protected async override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (request == null)
{
throw Error.ArgumentNull("request");
}
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
// This message handler is intended for helping with queries that return a null value, for example in a
// get request for a particular entity on an entity set, for a single valued navigation property or for
// a structural property of a given entity. The only case in which a data modification request will result
// in a 204 response status code, is when a primitive property is set to null through a PUT request to the
// property URL and in that case, the user can return the right status code himself.
ObjectContent content = response == null ? null : response.Content as ObjectContent;
if (request.Method == HttpMethod.Get && content != null && content.Value == null &&
response.StatusCode == HttpStatusCode.OK)
{
HttpStatusCode? newStatusCode = GetUpdatedResponseStatusCodeOrNull(request.ODataProperties().Path);
if (newStatusCode.HasValue)
{
response = request.CreateResponse(newStatusCode.Value);
}
}
return response;
}
示例14: CreateHttpRequestMessage
private static HttpRequestMessage CreateHttpRequestMessage(HttpActionDescriptor actionDescriptor, ODataRoute oDataRoute, HttpConfiguration httpConfig)
{
Contract.Requires(httpConfig != null);
Contract.Requires(oDataRoute != null);
Contract.Requires(httpConfig != null);
Contract.Ensures(Contract.Result<HttpRequestMessage>() != null);
Contract.Assume(oDataRoute.Constraints != null);
var httpRequestMessage = new HttpRequestMessage(actionDescriptor.SupportedHttpMethods.First(), "http://any/");
var requestContext = new HttpRequestContext
{
Configuration = httpConfig
};
httpRequestMessage.SetConfiguration(httpConfig);
httpRequestMessage.SetRequestContext(requestContext);
var httpRequestMessageProperties = httpRequestMessage.ODataProperties();
Contract.Assume(httpRequestMessageProperties != null);
httpRequestMessageProperties.Model = oDataRoute.GetEdmModel();
httpRequestMessageProperties.RouteName = oDataRoute.GetODataPathRouteConstraint().RouteName;
httpRequestMessageProperties.RoutingConventions = oDataRoute.GetODataPathRouteConstraint().RoutingConventions;
httpRequestMessageProperties.PathHandler = oDataRoute.GetODataPathRouteConstraint().PathHandler;
return httpRequestMessage;
}
示例15: GetModel
public override IEdmModel GetModel(Type elementClrType, HttpRequestMessage request,
HttpActionDescriptor actionDescriptor)
{
// Get model for the request
IEdmModel model = request.ODataProperties().Model;
if (model == null)
{
// user has not configured anything or has registered a model without the element type
// let's create one just for this type and cache it in the action descriptor
model = actionDescriptor.Properties.GetOrAdd("System.Web.OData.Model+" + elementClrType.FullName, _ =>
{
ODataConventionModelBuilder builder =
new ODataConventionModelBuilder(actionDescriptor.Configuration, isQueryCompositionMode: true);
builder.EnableLowerCamelCase();
EntityTypeConfiguration entityTypeConfiguration = builder.AddEntityType(elementClrType);
builder.AddEntitySet(elementClrType.Name, entityTypeConfiguration);
IEdmModel edmModel = builder.GetEdmModel();
Contract.Assert(edmModel != null);
return edmModel;
}) as IEdmModel;
}
Contract.Assert(model != null);
return model;
}