如何获取原始请求者当前 api NOT 的完整 url
How to get the full url of the current api NOT of the original requestor
背景
我在这个地址有一个本地后端 API:
客户端 UI 位于:
我需要的
UI 调用后端,列出所有可用订单。这包括有关每个订单附件的信息。它可能存在也可能不存在。如果有附件,您 应该 从 API 获得此响应,对于每个订单:
{
"orderReference": "123456",
"actions": { "download": "http://localhost:54641/orders/123456/download" }
}
如果没有可用的附件,操作将是 {}
。
不过
我做得到的是:
{
"orderReference": "123456",
"actions": { "download": "http://localhost:3000/orders/123456/download" }
}
当然不存在。
我现在拥有的
这段代码是构建完整的吗url,哪里出错了:
var baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority);
var uri = Url.Route("DownloadLabel", new {orderReference });
var fullUrl = $"{baseUrl}{uri}";
如,它 return 是请求者的完整 url 路径,而不是当前 API 的路径。
问题
What can I do to get the API url in the response?
所以,它应该 return 像这样:
找到了,我改用了httpcontext:
var baseUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
var uri = Url.Route("DownloadLabel", new {orderReference });
var fullUrl = $"{baseUrl}{uri}";
我猜你想要这个,
string fullUrl=HttpContext.Current.Request.Url.ToString();
var wantedUrl= fullUrl.SubString(0,fullUrl.IndexOf("/orders"))+"/orders/"+orderReference+"/download";
背景
我在这个地址有一个本地后端 API:
客户端 UI 位于:
我需要的
UI 调用后端,列出所有可用订单。这包括有关每个订单附件的信息。它可能存在也可能不存在。如果有附件,您 应该 从 API 获得此响应,对于每个订单:
{
"orderReference": "123456",
"actions": { "download": "http://localhost:54641/orders/123456/download" }
}
如果没有可用的附件,操作将是 {}
。
不过
我做得到的是:
{
"orderReference": "123456",
"actions": { "download": "http://localhost:3000/orders/123456/download" }
}
当然不存在。
我现在拥有的
这段代码是构建完整的吗url,哪里出错了:
var baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority);
var uri = Url.Route("DownloadLabel", new {orderReference });
var fullUrl = $"{baseUrl}{uri}";
如,它 return 是请求者的完整 url 路径,而不是当前 API 的路径。
问题
What can I do to get the API url in the response?
所以,它应该 return 像这样:
找到了,我改用了httpcontext:
var baseUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
var uri = Url.Route("DownloadLabel", new {orderReference });
var fullUrl = $"{baseUrl}{uri}";
我猜你想要这个,
string fullUrl=HttpContext.Current.Request.Url.ToString();
var wantedUrl= fullUrl.SubString(0,fullUrl.IndexOf("/orders"))+"/orders/"+orderReference+"/download";