为什么 Request.Url.Authority 返回内部路径而不是我的域

发布于 2024-12-24 00:05:26 字数 766 浏览 3 评论 0原文

我正在处理定于本周上线的应用程序的最后几期。我需要帮助来修改我的代码或向我们的托管商解释他们需要在 IIS/DNS 配置中修复哪些内容才能使此代码按预期工作。

代码如下:

public string BaseSiteUrl
{
    get
    {
        var c = this.ControllerContext.RequestContext.HttpContext;
        string baseUrl = c.Request.Url.Scheme + "://" + c.Request.Url.Authority
                       + c.Request.ApplicationPath.TrimEnd('/') + '/';
        return baseUrl;
    }
}

我在控制器中调用此函数,以生成一个持久保存到数据库的 URL。

当我在本地计算机上运行时它工作正常。然而,当它在测试版服务器上运行时,它不起作用。

测试版的预期结果。在测试版服务器上,这是一个名为 dr405 的应用程序

  • https://beta.sc-pa.com/dr405/

测试版的实际结果。 (为了安全起见,我将服务器/域名更改为您在 CAPS 中看到的内容)

  • http://SERVERNAME1.GROUP1.SUBGROUP.local/dr405/

I'm working through the final issues of an application set to go live this week. I need help to either modify my code or explain to our hosters what they need to fix in the IIS/DNS configurations to make this code work as expected.

Here is the code:

public string BaseSiteUrl
{
    get
    {
        var c = this.ControllerContext.RequestContext.HttpContext;
        string baseUrl = c.Request.Url.Scheme + "://" + c.Request.Url.Authority
                       + c.Request.ApplicationPath.TrimEnd('/') + '/';
        return baseUrl;
    }
}

I make a call to this in my Controller, to generate a url that gets persisted to a database.

It works fine when I run on my local machine. However, it does not work when it is run on the beta server.

Expected results on beta. On the beta server this is an application named dr405

  • https://beta.sc-pa.com/dr405/

The actual result on beta. (I changed the server/domain names to what you see in CAPS for security's sake)

  • http://SERVERNAME1.GROUP1.SUBGROUP.local/dr405/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜血缘 2024-12-31 00:05:26

我认为你不需要你写的方法。有一个 UrlHelper 添加扩展方法的类。要获取站点的基本 URL,您应该使用 Content() 方法如下:

var baseUrl = Url.Content("~/");

在您的示例中,它看起来像 http://SERVERNAME1.GROUP1.SUBGROUP.local/dr405/ 结果是一个内部的主机名。在您的开发计算机上,内部主机与您的面向公众的主机相匹配。您的托管提供商不太可能为您修改此设置,特别是如果它是共享托管解决方案。

I don't think you need the method you wrote. There is a UrlHelper class that adds extension methods. To get the base URL for your site you should be using the Content() method like this:

var baseUrl = Url.Content("~/");

In your example, it looks like the http://SERVERNAME1.GROUP1.SUBGROUP.local/dr405/ result is an internal host name. On your development machine the internal host matches your public facing one. Your hosting provider is unlikely to be able to modify this for you, especially if it's a shared hosting solution.

山有枢 2024-12-31 00:05:26

如果您使用负载平衡器或类似的设备,则可能值得检查服务器变量。在我们的例子中,我们做了这样的事情:

string hostName = Request.Headers["x-forwarded-host"];
hostName = string.IsNullOrEmpty(hostName) ? Request.Url.Host : hostName;

我不久前问的这个问题可能会引起兴趣:

Asp.net mvc 301从www.domain.com重定向到domain.com

If you're behind a load balancer or similar, it might be worth checking server variables. In our case we do something like this:

string hostName = Request.Headers["x-forwarded-host"];
hostName = string.IsNullOrEmpty(hostName) ? Request.Url.Host : hostName;

This question I asked a while ago might be of interest:

Asp.net mvc 301 redirect from www.domain.com to domain.com

拥抱我好吗 2024-12-31 00:05:26

由于时间限制以及需要推出该项目,我不得不将 url 的主要部分硬编码到应用程序中。做出改变后,我觉得一开始就试图让它变得动态是愚蠢的。我的意思是我们的域名应该多久更改一次?

Due to time constraints, and the need to get this project out the door, I had to resort to hardcoding the main part of the url into the application. After I made the change I felt stupid for trying to make it dynamic in the first place. I mean how often should our domain name change?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文