本文整理汇总了C#中System.Net.Http.HttpRequestMessage.GetUrlHelper方法的典型用法代码示例。如果您正苦于以下问题:C# HttpRequestMessage.GetUrlHelper方法的具体用法?C# HttpRequestMessage.GetUrlHelper怎么用?C# HttpRequestMessage.GetUrlHelper使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpRequestMessage
的用法示例。
在下文中一共展示了HttpRequestMessage.GetUrlHelper方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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.EntityContainers().Single().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();
configuration.Routes.MapHttpRoute(ODataRouteNames.PropertyNavigationWithCast, "{controller}({parentid})/{entitytype}/{navigationproperty}");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
request.Properties[HttpPropertyKeys.HttpRouteDataKey] = new HttpRouteData(new HttpRoute());
IEntitySetLinkBuilder linkBuilder = model.GetEntitySetLinkBuilder(vehiclesEdmEntitySet);
Uri uri = linkBuilder.BuildNavigationLink(
new EntityInstanceContext(model, vehiclesEdmEntitySet, carType, request.GetUrlHelper(), new Car { Model = 2009, Name = "Accord" }),
carManufacturerProperty);
Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/System.Web.Http.OData.Builder.TestModels.Car/Manufacturer", uri.AbsoluteUri);
}
示例2: GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType
public void GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType()
{
// Arrange
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
var cars = builder.EntitySet<Car>("cars");
var paintAction = cars.EntityType.Action("Paint");
IEdmModel model = builder.GetEdmModel();
var carsEdmSet = model.EntityContainers().Single().FindEntitySet("cars");
HttpConfiguration configuration = new HttpConfiguration();
string routeName = "Route";
configuration.Routes.MapODataRoute(routeName, null, model);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
request.SetConfiguration(configuration);
request.SetODataRouteName(routeName);
// Act
Uri link = ActionLinkGenerationConvention.GenerateActionLink(
new EntityInstanceContext()
{
EdmModel = model,
EntitySet = carsEdmSet,
EntityType = carsEdmSet.ElementType,
Url = request.GetUrlHelper(),
EntityInstance = new Car { Model = 2009, Name = "Accord" }
},
paintAction);
Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')/Paint", link.AbsoluteUri);
}
示例3: Apply_Doesnot_Override_UserConfiguration
public void Apply_Doesnot_Override_UserConfiguration()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
var vehicles = builder.EntitySet<Vehicle>("vehicles");
var car = builder.AddEntityType(typeof(Car));
var paintAction = vehicles.EntityType.Action("Paint");
paintAction.HasActionLink(ctxt => new Uri("http://localhost/ActionTestWorks"), followsConventions: false);
ActionLinkGenerationConvention convention = new ActionLinkGenerationConvention();
convention.Apply(paintAction, builder);
IEdmModel model = builder.GetEdmModel();
var vehiclesEdmSet = model.EntityContainer.FindEntitySet("vehicles");
var carEdmType = model.FindDeclaredType("System.Web.OData.Builder.TestModels.Car") as IEdmEntityType;
var paintEdmAction =
model.GetAvailableProcedures(
model.FindDeclaredType("System.Web.OData.Builder.TestModels.Car") as IEdmEntityType).Single()
as IEdmAction;
Assert.NotNull(paintEdmAction);
HttpConfiguration configuration = new HttpConfiguration();
configuration.Routes.MapODataServiceRoute(model);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
request.SetConfiguration(configuration);
ActionLinkBuilder actionLinkBuilder = model.GetActionLinkBuilder(paintEdmAction);
var serializerContext = new ODataSerializerContext { Model = model, NavigationSource = vehiclesEdmSet, Url = request.GetUrlHelper() };
var entityContext = new EntityInstanceContext(serializerContext, carEdmType.AsReference(), new Car { Model = 2009, Name = "Contoso" });
Uri link = actionLinkBuilder.BuildActionLink(entityContext);
Assert.Equal("http://localhost/ActionTestWorks", link.AbsoluteUri);
}
示例4: GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType
public void GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
var cars = builder.EntitySet<Car>("cars");
var paintAction = cars.EntityType.Action("Paint");
IEdmModel model = builder.GetEdmModel();
var carsEdmSet = model.EntityContainers().Single().FindEntitySet("cars");
HttpConfiguration configuration = new HttpConfiguration();
configuration.EnableOData(model);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
request.Properties[HttpPropertyKeys.HttpRouteDataKey] = new HttpRouteData(new HttpRoute());
Uri link = ActionLinkGenerationConvention.GenerateActionLink(
new EntityInstanceContext()
{
EdmModel = model,
EntitySet = carsEdmSet,
EntityType = carsEdmSet.ElementType,
UrlHelper = request.GetUrlHelper(),
PathHandler = new DefaultODataPathHandler(model),
EntityInstance = new Car { Model = 2009, Name = "Accord" }
},
paintAction);
Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')/Paint", link.AbsoluteUri);
}
示例5: SendAsync
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken) {
UrlHelper url = request.GetUrlHelper();
ServiceDocument doc = new ServiceDocument();
Workspace ws = new Workspace() {
Title = new TextSyndicationContent("My Site"),
BaseUri = new Uri(request.RequestUri.GetLeftPart(UriPartial.Authority))
};
ws.Collections.Add(GetPostsResourceCollectionInfo(url));
ws.Collections.Add(GetMediaResourceCollectionInfo(url));
doc.Workspaces.Add(ws);
HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK);
var formatter = new AtomPub10ServiceDocumentFormatter(doc);
var stream = new MemoryStream();
using (var writer = XmlWriter.Create(stream)) {
formatter.WriteTo(writer);
}
stream.Position = 0;
var content = new StreamContent(stream);
response.Content = content;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/atomsvc+xml");
return Task.FromResult(response);
}
示例6: GenerateLinksForTopic
private static Topic GenerateLinksForTopic(Topic topic, HttpRequestMessage request)
{
var routeData = request.GetRouteData();
var controller = routeData.Values["controller"];
var urlHelper = request.GetUrlHelper();
topic.SelfLink = new Uri(urlHelper.Route("DefaultApi", new { controller = controller, id = topic.Id }), UriKind.Relative);
if (topic.Comments != null && topic.Comments.Count > 0)
{
topic.Comments = new List<Comment>(topic.Comments.Select((cmt) =>
{
cmt.SelfLink = new Uri(urlHelper.Route("DefaultApi", new { controller = controller, id = cmt.Id }), UriKind.Relative);
cmt.TopicLink = new Uri(urlHelper.Route("DefaultApi", new { controller = "topics", id = cmt.TopicId }), UriKind.Relative);
return cmt;
}));
}
if (topic.Tags != null && topic.Tags.Count > 0)
{
topic.Tags = new List<Tag>(topic.Tags.Select((tag) =>
{
tag.SelfLink = new Uri(urlHelper.Route("DefaultApi", new { controller = controller, tags = tag.Name }), UriKind.Relative);
return tag;
}));
}
return topic;
}
示例7: SendAsync
protected override async System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
var resultsUri = request.GetUrlHelper().Link("Profiler", new { id = _profiler.Id });
var response = await base.SendAsync(request, cancellationToken);
response.Headers.Add(Constants.ProfilerResultsHeaderName, resultsUri);
return response;
}
示例8: 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);
}
示例9: EmbeddedHtmlResult
public EmbeddedHtmlResult(HttpRequestMessage request, string file, OAuth2Configuration oauthConfig = null)
{
this.path = request.GetOwinContext().Request.PathBase.Value;
this.file = file;
this.oauthConfig = oauthConfig;
if (oauthConfig != null && oauthConfig.AutomaticallyRenewToken)
{
this.frameCallbackUrl = request.GetUrlHelper().Link(Constants.RouteNames.OAuthFrameCallback, null);
}
}
示例10: GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType
public void GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
var cars = builder.EntitySet<Car>("cars");
var paintAction = cars.EntityType.Action("Paint");
IEdmModel model = builder.GetEdmModel();
var carsEdmSet = model.EntityContainers().Single().FindEntitySet("cars");
HttpConfiguration configuration = new HttpConfiguration();
configuration.Routes.MapHttpRoute(ODataRouteNames.InvokeBoundAction, "{controller}({boundId})/{odataAction}");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
request.Properties[HttpPropertyKeys.HttpRouteDataKey] = new HttpRouteData(new HttpRoute());
Uri link = ActionLinkGenerationConvention.GenerateActionLink(
new EntityInstanceContext(model, carsEdmSet, carsEdmSet.ElementType, request.GetUrlHelper(), new Car { Model = 2009, Name = "Accord" }),
paintAction);
Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')/Paint", link.AbsoluteUri);
}
示例11: GenerateActionLink_GeneratesLinkWithCast_IfEntitySetTypeDoesnotMatchActionEntityType
public void GenerateActionLink_GeneratesLinkWithCast_IfEntitySetTypeDoesnotMatchActionEntityType()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
var vehicles = builder.EntitySet<Vehicle>("vehicles");
var car = builder.Entity<Car>();
var paintAction = car.Action("Paint");
IEdmModel model = builder.GetEdmModel();
var vehiclesEdmSet = model.EntityContainers().Single().FindEntitySet("vehicles");
var carEdmType = model.FindDeclaredType("System.Web.Http.OData.Builder.TestModels.Car") as IEdmEntityType;
HttpConfiguration configuration = new HttpConfiguration();
configuration.Routes.MapHttpRoute(ODataRouteNames.InvokeBoundActionWithCast, "{controller}({boundId})/{entityType}/{odataAction}");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
request.Properties[HttpPropertyKeys.HttpRouteDataKey] = new HttpRouteData(new HttpRoute());
Uri link = ActionLinkGenerationConvention.GenerateActionLink(
new EntityInstanceContext(model, vehiclesEdmSet, carEdmType, request.GetUrlHelper(), new Car { Model = 2009, Name = "Accord" }),
paintAction);
Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/System.Web.Http.OData.Builder.TestModels.Car/Paint", link.AbsoluteUri);
}
示例12: GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeDerivesFromActionEntityType
public void GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeDerivesFromActionEntityType()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
var cars = builder.EntitySet<Car>("cars");
var paintAction = builder.Entity<Vehicle>().Action("Paint");
IEdmModel model = builder.GetEdmModel();
var carsEdmSet = model.EntityContainers().Single().FindEntitySet("cars");
HttpConfiguration configuration = new HttpConfiguration();
string routeName = "Route";
configuration.Routes.MapODataRoute(routeName, null, model);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
request.SetConfiguration(configuration);
request.SetODataRouteName(routeName);
var serializerContext = new ODataSerializerContext { Model = model, EntitySet = carsEdmSet, Url = request.GetUrlHelper() };
var entityContext = new EntityInstanceContext(serializerContext, carsEdmSet.ElementType.AsReference(), new Car { Model = 2009, Name = "Accord" });
Uri link = ActionLinkGenerationConvention.GenerateActionLink(entityContext, paintAction);
Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')/Paint", link.AbsoluteUri);
}
示例13: 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);
}
示例14: SelfLinksGenerationConvention_Throws_If_RouteInCorrect
public void SelfLinksGenerationConvention_Throws_If_RouteInCorrect()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
var motorcycles = builder.EntitySet<Motorcycle>("motorcycles");
IEdmModel model = builder.GetEdmModel();
IEdmEntitySet vehiclesEdmEntitySet = model.EntityContainers().Single().EntitySets().Single();
IEdmEntityType sportbikeType = model.AssertHasEntityType(typeof(SportBike));
HttpConfiguration configuration = new HttpConfiguration();
configuration.Routes.MapHttpRoute(ODataRouteNames.GetById, "{Cntroller}({id})");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
request.Properties[HttpPropertyKeys.HttpRouteDataKey] = new HttpRouteData(new HttpRoute());
IEntitySetLinkBuilder linkBuilder = model.GetEntitySetLinkBuilder(vehiclesEdmEntitySet);
Assert.Throws<InvalidOperationException>(
() => linkBuilder.BuildEditLink(new EntityInstanceContext(model, vehiclesEdmEntitySet, sportbikeType, request.GetUrlHelper(), new SportBike { Model = 2009, Name = "Ninja" })),
"EditLink generation failed. Check that you have the 'OData.GetById' route correctly registered.");
}
示例15: NavigationLinksGenerationConvention_GeneratesLinksWithoutCast_ForBaseProperties
public void NavigationLinksGenerationConvention_GeneratesLinksWithoutCast_ForBaseProperties()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<SportBike>("vehicles");
builder.EntitySet<Manufacturer>("manufacturers");
IEdmModel model = builder.GetEdmModel();
IEdmEntitySet vehiclesEdmEntitySet = model.EntityContainer.FindEntitySet("vehicles");
IEdmEntityType sportbikeType = model.AssertHasEntityType(typeof(SportBike));
IEdmNavigationProperty motorcycleManufacturerProperty = sportbikeType.AssertHasNavigationProperty(model, "Manufacturer", typeof(MotorcycleManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);
HttpConfiguration configuration = new HttpConfiguration();
string routeName = "Route";
configuration.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, sportbikeType.AsReference(), new SportBike { Model = 2009, Name = "Ninja" });
Uri uri = linkBuilder.BuildNavigationLink(entityContext, motorcycleManufacturerProperty, ODataMetadataLevel.FullMetadata);
Assert.Equal("http://localhost/vehicles(Model=2009,Name='Ninja')/Manufacturer", uri.AbsoluteUri);
}