asp.net-mvc 如何在 MVC Razor 中正确编码指向外部 URL 的链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23094174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to properly encode links to external URL in MVC Razor
提问by Eugene Goldberg
This view suppose to show a list of hyperlinks, each pointing to an external URL. The goal is for the user to click one of these links and have their browser open a new tab with the selected URL. Currently I have the following markup:
该视图假设显示一个超链接列表,每个超链接都指向一个外部 URL。目标是让用户单击这些链接之一,并让他们的浏览器打开一个带有所选 URL 的新选项卡。目前我有以下标记:
@Html.ActionLink("SomeSite", "http://subdomain.mydomain.com/SomeSite")
This markup produces:
此标记产生:
http://localhost:58980/AccessInstance/http%3a/subdomain.mydomain.com/SomeSite
instead of :
代替 :
http://subdomain.mydomain.com/SomeSite
What can I change in my markup to make this work as I expect?
我可以更改我的标记以使其按预期工作吗?
回答by rossipedia
You don't need to use @Html.ActionLinkfor that. Just use a plain A tag:
你不需要为此使用@Html.ActionLink。只需使用一个普通的 A 标签:
<a href="http://subdomain.mydomain.com/SomeSite">SomeSite</a>
Html.ActionLinkis specifically for generating links to actions defined in MVC controllers, in the same app. Since you're linking to an absolute URL, you don't need any of the functionality that Html.ActionLinkprovides.
Html.ActionLink专门用于在同一应用程序中生成指向 MVC 控制器中定义的操作的链接。由于您要链接到绝对 URL,因此您不需要任何提供的功能Html.ActionLink。
回答by shaijut
Two ways :
两种方式:
1.update the database column with full link:
1.使用完整链接更新数据库列:
eg SQL:
例如SQL:
update ProductTable set LinkColumn='http://www.example.com/Product/Mobiles' where ID=123
In asp mvc view
在 asp mvc 视图中
<a href="@model.ProductLink">View</a>
2.Hardcode the httppart and list from model
2.对http模型中的零件和列表进行硬编码
<a href="http://@model.ProductLink">View</a>
hope helps someone.
希望能帮助某人。
回答by Tom Stickel
While a ViewBag is overused and not the best choice most of the time this is something that I had done when inheriting someone else's mvc app to do a quick fix for a URL that I needed to redirect to with a specific dynamically changing querystring parameter
虽然 ViewBag 被过度使用并且在大多数情况下不是最佳选择,但这是我在继承其他人的 mvc 应用程序以快速修复我需要使用特定动态更改的查询字符串参数重定向到的 URL 时所做的事情
<a target="_parent" href="http://localhost:56332/services/@ViewBag.factory">View Service</a>
回答by saktiprasad swain
Here to display link that are clickable in index page
此处显示可在索引页面中单击的链接
<td>
@Html.ActionLink(item.FileName, "../Uploads/Catalogue/"+item.FileName)
</td>
回答by Zinov
You need to take into account your RouteConfiguration.
您需要考虑您的 RouteConfiguration。
routes.MapRoute( name: "Default", url: "{controller}/{action}"
路线.MapRoute(名称:“默认”,网址:“{controller}/{action}”
because you are specifying the action link as the entire link that you want to redirect. I would recommend that you use the @rossipedia answer because you can make tricky things like putting a span inside the link
因为您将操作链接指定为要重定向的整个链接。我建议您使用@rossipedia 的答案,因为您可以做一些棘手的事情,例如在链接中放置一个跨度

