【问题标题】:in razor how to have a link to an external url from the database在剃须刀中,如何从数据库中获得指向外部 url 的链接
【发布时间】:2018-09-18 16:15:34
【问题描述】:

在我的剃须刀页面中。

<p>
    <span>Website: </span>
    <span>
        <a href="@(Url.Encode(Model.PrimaryInfo.WebsiteUrl))" target="_blank">@Model.PrimaryInfo.WebsiteUrl</a>
    </span>
</p>

当我检查我看到的元素时。 页面显示为

网址:https://www.mywebsite.org/

当我检查元素时,我得到了

<a href="https%3a%2f%2fwww.mywebsite.org%2f" target="_blank">https://www.mywebsite.org/</a>

当我点击链接时,我得到了

[HttpException (0x80004005): 有潜在危险的 Request.Path 从客户端检测到值 (:)。]
System.Web.HttpRequest.ValidateInputIfRequiredByConfig() +9939972
System.Web.PipelineStepManager.ValidateHelper(HttpContext 上下文) +53

【问题讨论】:

标签: c# asp.net asp.net-mvc


【解决方案1】:

尝试使用这个

public static class LinkHelper{
public static string ExternalLink(this HtmlHelper helper, string url, string text)
    {
        return String.Format("<a href='http://{0}' target="_blank">{1}</a>", url,text);
}}

在视野中

@Html.ExternalLink("www.google.com", "Google")

【讨论】:

    【解决方案2】:

    这是您的清单:

    1. 未使用非 unicode 数据类型:验证您将 URL 作为 NVarChar 类型存储在数据库中并且不使用 VarChar

    2. 无需编码:移除编码。

    3. 根据 .Net 验证您没有违反请求验证方案。 请参考hanslman文章: https://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx

    【讨论】:

      猜你喜欢
      • 2016-10-17
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 2023-04-09
      • 2020-09-21
      • 2012-08-03
      相关资源
      最近更新 更多