I use WebAPI 2.2 and Microsoft.AspNet.OData 5.7.0 to create an OData service that supports paging.
When hosted in a production environment, the WebAPI lives on a server that is not displayed externally, so various links returned in the OData response, such as @odata.contextand @odata.nextLink, point to an internal IP address, for example. http://192.168.X.X/<AccountName>/api/...etc.
I was able to change Request.ODataProperties().NextLinkby following some logic in each ODataController method to replace the internal URL with an external URL, for example https://account-name.domain.com/api/..., but this is very inconvenient and it only fixes NextLinks.
Is there any way to set the external host name while configuring the OData service? I saw a property Request.ODataProperties().Pathand I wonder if it is possible to set the base path when called config.MapODataServiceRoute("odata", "odata", GetModel());or in the implementation GetModel()using, for example ODataConventionModelBuilder,?
The UPDATE: . The best solution I've come up with so far is to create BaseODataControllerone that overrides the method Initializeand checks to see if there is Request.RequestUri.Host.StartsWith("beginning-of-known-internal-IP-address"), and then executes RequestUri, rewrite it like this:
var externalAddress = ConfigClient.Get().ExternalAddress;
var account = ConfigClient.Get().Id;
var uriToReplace = new Uri(new Uri("http://" + Request.RequestUri.Host), account);
string originalUri = Request.RequestUri.AbsoluteUri;
Request.RequestUri = new Uri(Request.RequestUri.AbsoluteUri.Replace(uriToReplace.AbsoluteUri, externalAddress));
string newUri = Request.RequestUri.AbsoluteUri;
this.GetLogger().Info($"Request URI was rewritten from {originalUri} to {newUri}");
This captures URLs perfectly @odata.nextLinkfor all controllers, but for some reason the URLs @odata.contextstill get the part AccountName(e.g. https://account-name.domain.com/AccountName/api/odata/ $ metadata # ControllerName) therefore they still do not work.