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.pdfBUT 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!
You need to store the relative url in a hidden field:
<input id='hidPartialUrl' type='hidden' value='<%:ResolveUrl("~/") %>'/>
All links should be relative to the root of the site and have a common class name:
<a href='/FolderA/myfile.pdf' class='fixThisLink'>PDF</a>
Then when you load it into the page you do:
var hiddenHref = document.getElementById('hidPartialUrl').href;
var links = document.getElementsByClassName('fixThisLink');
for(var i = 0; i < links.length; i++) {
links[i].href = hiddenHref + links[i].href;
}
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.
精彩评论