超链接相对于当前的网址不是用户控制的路径 [英] Hyperlink relative to current URL not path of user control
问题描述
我有一个网页在我的网站,在这个例子的目的,可以说的http://myhost/Pages/Default.aspx 。在Default.aspx的我有一个名为MyControl.ascx用户控制,生活在/Controls/MyControl.ascx。因此,树看起来像这样
I have a page on my site, for purposes of this example lets say http://myhost/Pages/Default.aspx. On Default.aspx I have a user control called MyControl.ascx which lives in /Controls/MyControl.ascx. So the tree looks something like this
- 根
- 页
- 的Default.aspx
- MyControl.ascx
每当我放在MyControl.ascx HyperLink控件并指定NavigateUrl,这个路径是相对于对照,而不是页面的URL。因此,举例来说,如果在NavigateUrl我指定的AboutMe.aspx,该URL将被渲染为 HTTP://myhost/Controls/AboutMe.aspx 而不是的http://myhost/Pages/AboutMe.aspx 。有没有什么办法可以使这相对于页面的网址?我已经试过这里的解决方案:<一href=\"http://stackoverflow.com/questions/2604636/relative-path-from-an-asp-net-user-control-navigateurl\">http://stackoverflow.com/questions/2604636/relative-path-from-an-asp-net-user-control-navigateurl但它并没有为我工作。
Whenever I place a HyperLink control on MyControl.ascx and specify a NavigateUrl, this path is relative to the control, not the URL of the page. So for instance if in NavigateUrl I specified "AboutMe.aspx", the URL would be rendered as http://myhost/Controls/AboutMe.aspx instead of http://myhost/Pages/AboutMe.aspx. Is there any way I can make this relative to the page URL? I've tried the solution here: http://stackoverflow.com/questions/2604636/relative-path-from-an-asp-net-user-control-navigateurl but it didn't work for me.
修改
要澄清一下,我想这是足够通用的,所以,如果我不知道什么道路是解决办法是可行的。所以,我真的不希望哈日code〜/页/ Default.aspx的在NavigateUrl
To clarify, I'd like this to be generic enough so that if I didn't know what the path was the solution would work. So I don't really want to harcode "~/Pages/Default.aspx" in the NavigateUrl
推荐答案
您有一堆选项:
-
您可以硬code。使用
〜
运营商,给你的根,然后像从那里把它定义的网址:〜/页/ AboutMe.aspx
。请记住,在〜
运营商只对服务器控件和服务器code认可。
You could hardcode the Url using the
~
operator which give you the root and then define it from there like:~/Pages/AboutMe.aspx
. Keep in mind that the~
operator is recognized only for server controls and in server code.
您也可以使用
..
来让你到你想去的地方,因为它会浏览备份的文件夹结构,如:../页/ AboutMe.aspx
You could also use the
..
to get you to where you want as it will navigate back up the folder structure like:../Pages/AboutMe.aspx
您可以创建一个辅助的方法来有效的根返回到你的Pages文件夹,图片,JavaScript,等等...
You could create a helper methods to return you a valid root to your Pages folder, Images, Javascript, etc...
的
的Htt prequest.ApplicationPath
会得到,你可以用它来构建关闭服务器上的虚拟应用程序的根路径。The
HttpRequest.ApplicationPath
will get your the virtual application's root path on the server which you can use to build off.有关径处理选项的详细信息,请阅读这篇文章在MSDN上:
For more information on Pathing options you should read this article on MSDN:
编辑:根据您的编辑你的问题,则应通过在相对URL到你通过属性的用户控件。让使用该控件的页面中定义它需要资源的相对路径。
Based on your edit to your question, you should then pass in the relative URL into you user control via a property. Let the page that uses the control define the relative path to the resource it will require.
这篇关于超链接相对于当前的网址不是用户控制的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 页