如何在 html 链接元素中为 IIS 上运行的 Web 应用程序创建相对 Url?
假设我有一个网络应用程序:
www.mymvcsite.com/MyVirtualDirectory/Controller/Action
在我看来,我有一个 。问题是整个 Html 来自数据库!啊哈!你认为这就像 @Url.Content() 一样简单......好吧,这不起作用,因为 html 是动态的,事实上我不知道标记中甚至有链接元素,我盲目地将它输出到看法。
那么我需要在 href 中放入什么才能正确解析 Url。
IE。 “myfile.pdf”位于...
www.mymvcsite.com/MyVirtualDirectory/FolderA/myfile.pdf
但由于某种原因,在浏览器中,网址最终变成
“www.mymvcsite.com/FolderA/myfile.pdf”
请注意,“MyVirtualDirectory”丢失了!我什至尝试在网址中添加“~”,但这也不起作用。
谢谢!
Let say I have a web application :
www.mymvcsite.com/MyVirtualDirectory/Controller/Action
In my view I have a <a href="/FolderA/myfile.pdf">
. Problem is the entire Html is coming from from a database! Aha! You thought this was as easy as @Url.Content()... well that doesn't work cause the html is dynamic in fact I have no idea that there is even link element in the markup I'm blindly outputting it to the view.
So what do I need to put in the href in order for it to resolve the Url properly.
Ie. "myfile.pdf" is located at...
www.mymvcsite.com/MyVirtualDirectory/FolderA/myfile.pdf
BUT for some reason in the browser the url ends up being
"www.mymvcsite.com/FolderA/myfile.pdf"
Note that the "MyVirtualDirectory" is missing! I even tried adding a "~" to the Url but that doesn't work either.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将相对 url 存储在隐藏字段中:
所有链接都应该相对于网站的根目录,并具有通用的类名:
然后,当您将其加载到页面中时,您需要执行以下操作:
在返回的每个链接上设置 href 属性包含网址的第一部分。我会将您网址的第一部分存储在隐藏字段中。
You need to store the relative url in a hidden field:
All links should be relative to the root of the site and have a common class name:
Then when you load it into the page you do:
Set the href property on each one returned to include the first part of your url. I would store the first part of your url in a hidden field.