简体   繁体   English

在href标签中加载到外部网站的链接

[英]Load link to external website in href tag

I have a column in database that hold links to websites its name is "link" in my razor view I need to read what is found in this column in an anchor tag so if the column hold "www.website.com" 我在数据库中持有相关网站的链接它的名字是“链接”在我的Razor视图我需要阅读什么在这一栏发现一个锚标记一个列,因此如果列保持"www.website.com"

<a href="@Model.link">click</a>

I expect this to result 我希望这会导致

<a href="www.soukcom.com">click</a>

but when I click on it it link me to 但是当我单击它时,它会将我链接到

http://localhost:11767/en/Home/www.soukcom.com

How can I linked to the correct website link that found in database 如何链接到数据库中找到的正确网站链接

Is there any chance that you have a leading / character at the beginning of your Model.link property? 您是否有可能在Model.link属性的开头有一个前导/字符? If so - this would cause Razor to see this as a relative path and would explain why it's pointing to that specific location. 如果是这样,这将使Razor将此视为相对路径,并解释其为何指向该特定位置。

If you are pulling these values from a database, then you might want to consider sanitizing any relative URLs within your ViewModel by using something like the following : 如果要从数据库中提取这些值,则可能需要考虑使用以下类似方法清理ViewModel中的所有相对URL:

public string NonRelativeLink => link.TrimStart('/');

Then simply use this updated property : 然后只需使用此更新的属性:

<a href="@Model.NonRelativeLink">click</a>

All you have to do is update the address to an HTTP(S) Protocol 您要做的就是将地址更新为HTTP(S)协议

Enjoy coding ! 享受编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM