母版页编译和绝对 URL
已更新 03/04/09
为了响应一些评论,母版页中的示例如下所示。 这不是 asp.net 控件,这是硬编码的 html
<a href="http://www.MYDOMAIN.com/about_us.asp" accesskey="u"><span class="topleft"><span class="bottomleft">About us</span></span></a>
这在生产服务器上呈现,因为
<a href="http://www.NEWDOMAIN.com/about_us.asp" accesskey="u"><span class="topleft"><span class="bottomleft">About us</span></span></a>
MYDOMAIN 是我们主站点的真实域名,NEWDOMAIN 是指向同一站点的完全有效的 DNS 条目。
更新 02/04/09
所有 URL 都是绝对的,因为它们以 http:// 开头,
我不认为这可能是浏览器问题,因为实际呈现的源代码(通过查看源代码查看)已更改。 检查 IE7/8 和 Firefox 3 并发现相同的行为。
原始问题
我有一个 ASP.Net 2.0 应用程序,它有多个母版页。 这本质上是模拟的,看起来与我们的主网站一模一样,但因为它运行在不同的服务器上,所以菜单项等的所有 URL 都被赋予了我们主网站的绝对 URL。
这在我的开发机器上工作得很好,但在生产服务器上,所有绝对的 URL 在运行时都会发生变化,但单击时它们仍然会出现在相同的页面上。
这是 DNS 问题吗? 当母版页和内容合并时,ASP.Net 是否会对 URL 进行一些 DNS 解析? 如果是这样,那么为什么它在我的本地计算机上没有相同的效果,它们位于同一个域中。
UPDATED 03/04/09
In response to some comments, a sample from the master page looks like this. This is not an asp.net control, this is hard coded html
<a href="http://www.MYDOMAIN.com/about_us.asp" accesskey="u"><span class="topleft"><span class="bottomleft">About us</span></span></a>
This renders on the production server as
<a href="http://www.NEWDOMAIN.com/about_us.asp" accesskey="u"><span class="topleft"><span class="bottomleft">About us</span></span></a>
MYDOMAIN is the true domain name of our main site, NEWDOMAIN is a perfectly valid DNS entry which points to the same site.
UPDATED 02/04/09
All the URLS are absolute in the sense that they begin http://
I don't think this can be a browser issue as the actual rendered source code (viewed via view source) has been changed. Checked in both IE7/8 and Firefox 3 and witnessed same behaviour.
Original Question
I have an ASP.Net 2.0 application which has several master pages. This is essentially mocked up to look exactly like our main website, but because it runs on a different server all of the URLs for menu items etc are given absolute URLs to our main site.
This works fine on my development machine, but on the production server all of the URLs which are absolute are changing at runtime, but they still end up at the same pages when clicked.
Is this a DNS issue? Does ASP.Net do some DNS resoltion of URL's when the master page and content are merged? If so then why does it not have the same effect on my local machine, they are on the same domain.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这不是 DNS 问题,ASP.net 不执行任何 DNS 解析。 这是您查看页面的浏览器的全部责任。
但是,有几种情况可能会导致页面标记中提供的 URL 不一致,客户端浏览器可能会以不同的方式解释这些 URL。
浏览器始终会以相同的方式解释以“http://”开头的 URL - 它是绝对 URL,因此目标始终会解析为相同的内容。 确保您的主站点的所有 URL 都以“http://”开头。
以“www”开头的 URL。 (无 http://)将被视为相对 URL - 即,如果包含该 URL 的页面位于 http://www .google.com,您实际上是在请求 http://www.google.com/YourUrl 。 您会发现这几乎肯定不是您正在寻找的行为。
以前导斜杠 (/) 开头的 URL 将被视为当前域中的绝对 URL。 例如,Google 中的“/Tools”将导致对“http://www.google.com/ 的请求工具”。 如果没有前导斜杠,浏览器会将 URL 视为相对于当前正在查看的页面(即,当您查看“en”文件夹中的页面时,“Tools”的 URL 将导致对“en”的请求/Tools”。
我认为这就是您出现问题的地方。为了保持一致的行为,我发现确保所有 URL 都以前导斜杠开头是一个很好的经验法则。如果您想确保 ASP 代码生成的所有超链接都是正确,请使用波浪号(ASP 将替换为应用程序根文件夹的路径):
这样,无论您使用 Cassini、IIS 中的网站还是您的页面在站点结构中的位置都无关紧要。 IIS 中的虚拟目录 - URL 将始终解析为正确的地址
如果您想要输出不是服务器控件属性的 URL,请使用 ResolveUrl 方法:
希望这会有所帮助。
No, it's not a DNS issue, and ASP.net doesn't do any DNS resolution. That's all the responsibility of the browser you're viewing the page in.
However, there are several circumstances that can lead to inconsistent URLs being served in the page's mark-up, which may be interpreted differently by the client's browser.
Browser's will always interpret a URL beginning "http://" the same way - it's an absolute URL, so the destination will always resolve to the same thing. Make sure all your URLs to your main site begin "http://".
URLs beginning "www." (no http://) will be treated as relative URLs - i.e. if the page containing the URL is at http://www.google.com, you're essentially asking for http://www.google.com/YourUrl. You'll find this almost certainly isn't the behaviour you're looking for.
URLs beginning with a leading slash (/) will be treated as absolute on the current domain. For example "/Tools", within Google will result in a request to "http://www.google.com/Tools". If there's no leading slash, the browser will treated the URL as being relative to the page currently being viewed (i.e. a URL of "Tools" when you're viewing a page in the "en" folder would result in a request to "en/Tools".
I think this is where your problems are arising. For consistent behaviour, I find it's a good rule of thumb to ensure all URLs begin with a leading slash. If you want to ensure all your hyperlinks generated by your ASP code are correct, use the tilde (which ASP will replace with the path to the application root folder):
This way, it doesn't matter where your page is in your site structure, whether you're using Cassini, a web site in IIS or a virtual directory in IIS - the URL will always resolve to the correct address.
If you want to output a URL that isn't a property of a server control, use the ResolveUrl method:
Hope this helps.
我认为“绝对”是指它们以“/”而不是文件夹名称开头?
如果您使用的是 ASP.NET 超链接< /a> 控制,那么这些将倾向于修改为从应用程序根目录启动。
编辑评论
您能给我们举一个如何更改网址的示例吗? 即从 http://www.example.com/somepage.aspx 到http://www.example.com/trackingpage.aspx?somequerysting -或者域名正在改变? 或者是其他东西?
你说“他们最终仍然在相同的页面上”——很明显事情正在发挥作用。 您是否在生产服务器上的 web.config 中注册了任何 HttpHandler,这些 HttpHandler 可以为您修改 URL,以便它们都通过某个日志系统? 即从服务器获取响应,处理生成的 HTML,修改所有链接 - 简单的锚标记和超链接控件是否会发生这种情况?
您是否使用在 PreRender 或 Render 中执行其他步骤的自定义基页,这些步骤在生产过程中与更改 URL 的开发人员计算机不同?
By "absolute" I assume you mean they start with a "/" rather than a folder name?
If you are using the ASP.NET hyperlink control, then these will tend to modified to start at the application root.
Edit for comment
Can you give us an example of how the urls are being changed? i.e. From http://www.example.com/somepage.aspx to http://www.example.com/trackingpage.aspx?somequerysting - or is the domain changing? or something else?
You say "they still end up at the same pages" - so clearly things are working. Have you got any HttpHandlers registered in the web.config on your production servers that could be modifying the URLs for you so that they all go through some logging system? I.e. taking the response from the server, processing the resultant HTML, modifying all links - does it happen with simple anchor tags as well as Hyperlink controls?
Are you using a custom base page that is performing additional steps in PreRender or Render that's different on production to your developer machine that is changing the URLs?