UriHelper如何获取生产URL路径

时间:2019-04-16 04:12:50

标签: c#

var displayUrl = UriHelper.GetDisplayUrl(Request);
var urlBuilder = new UriBuilder(displayUrl) {  Query = null, Fragment = null };

string _activation_url = urlBuilder.ToString().Substring(0, urlBuilder.ToString().LastIndexOf("/")) +"/this_is_my_link.html";

我希望获得正确的uri生产路径,但我仍然会得到

http://localhost:5000/api/mdc/this_is_my_link.html

我将其部署在centos 7上

请帮助我。

谢谢

2 个答案:

答案 0 :(得分:0)

在asp核心上使用

absoluteUri = string.Concat(
                           request.Scheme,
                           "://",
                           request.Host.ToUriComponent(),
                           request.PathBase.ToUriComponent(),
                           request.Path.ToUriComponent(),
                           request.QueryString.ToUriComponent());

或者您可以选择 Getting absolute URLs using ASP.NET Core

答案 1 :(得分:0)

如果您使用的是反向代理,则应从Microsoft阅读this guide

本质上,您的反向代理应将以下标头提供给ASP.NET Core应用程序:

  • X-Forwarded-For-客户端IP
  • X-Forwarded-Host-客户端的Host标头(例如www.example.com:80
  • X-Forwarded-Proto-协议(例如HTTPS

然后,您需要配置ASP.NET Core应用程序以接受它们。您可以通过在启动程序的Configure方法中调用app.UseForwardedHeaders()方法来实现。

By default(如果我正确阅读了文档)UseForwardedHeaders(如上所述)将接受来自X-Forwarded-For反向代理的X-Forwarded-Protolocalhost

如果情况复杂得多,则必须配置所需的标头/受信任的反向代理:

var forwardedOptions = new ForwardedHeadersOptions()
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedHost | ForwardedHeaders.XForwardedProto // allow for, host, and proto (ForwardedHeaders.All also works here)
};

// if it's a single IP or a set of IPs but not a whole subnet
forwardedOptions.KnownProxies.Add(IPAddress.Parse("192.168.0.5"));

// if it's a whole subnet
forwardedOptions.KnownNetworks.Add(new IPNetwork("192.168.0.1", 24)); // 192.168.0.1 - 192.168.0.254

app.UseForwardedHeaders(forwardedOptions);

还要注意,根据您使用的反向代理,您可能需要在反向代理上进行配置