哪种方法用于在文本中注入URL?

huangapple go评论80阅读模式
英文:

Which approach for injecting urls in text?

问题

我有一个使用C#的任务。它是现有Web表单项目的一部分。

我想要做的是:

  • 在页面的<main></main>标签内(具有runat="server"属性),
  • 查找包含页面ID的<a></a>标签
    (例如,data-page-id="12345");
  • 从数据库中获取每个PageId的完全限定URL; // 这部分比较容易
  • 将URL注入回文本/控件中(href="mysite.com/an-seo-worthy-url")。

我遇到困难的地方是初始方法。我是否使用FindControls()功能?; 正则表达式? HtmlAgilityPack?这种任务是否有首选方法,还是有其他方法?

有没有其他人做过类似的事情,可以指引我朝特定方向前进?

英文:

I have a task using c#. It's part of an existing web forms project.

What I want to do is:

  • inside the page's &lt;main&gt;&lt;/main&gt; tag (which has a runat=&quot;server&quot;),
  • find the &lt;a&gt;&lt;/a&gt; tags which have an attribute containing the page-id
    (E.g. data-page-id=&quot;12345&quot;);
  • fetch the fully qualified urls from the database for each PageId; // the easy bit
  • inject the urls back into the text/controls (href="mysite.com/an-seo-worthy-url").

Where I'm struggling is with the initial approach. Do I use the FindControls() functionality?; Regex? HtmlAgilityPack? Is any one of these preferred for this kind of task, or is there another way?

Anyone else done something similar, that can point me in a particular direction?

答案1

得分: 1

以下是您要翻译的内容:

好的,目标是在页面上有超链接,但链接的解析要从数据库中进行。

我不会在页面加载时替换所有超链接,因为这将导致数据库成本过高。假设我们在页面上有10个链接。如果我们在页面加载时替换这些链接,那就需要进行10次数据库操作。这相当昂贵,因为假设我们网站上有20个用户,那就需要进行200次数据库操作。这是非常多的数据库查询和提取操作。尤其是因为用户还没有点击任何链接!

因此,我建议使用并在页面中嵌入一个服务器端的超链接控件。

比如,像这样(2个示例链接按钮)

<asp:LinkButton ID="L1" runat="server"
    OnClick="mylinkresolve_Click"
    data-page-id="12345"                
    >My Link Text</asp:LinkButton>

<br />

<asp:LinkButton ID="L2" runat="server"
    OnClick="mylinkresolve_Click"
    data-page-id="6789"                
    >My Link Text2 </asp:LinkButton>

现在,您可以考虑使用上面的ID,但无论哪种方式都可以,不太重要。在上面的示例中,我们使用了data-page-id。

那么,现在在代码后台,我们有一个所有链接按钮都指向的统一的方法。

代码如下:

protected void mylinkresolve_Click(object sender, EventArgs e)
{
    LinkButton btn = (LinkButton)sender;
    string strSQL = @"SELECT Hlink FROM tblMyLinks 
                    WHERE LinkID = @LinkID";

    SqlCommand cmdSQL = new SqlCommand(strSQL);
    cmdSQL.Parameters.Add("@LinkID", SqlDbType.NVarChar).Value = btn.Attributes["data-page-id"];

    DataTable rstLink = General.MyRstP(cmdSQL);
    if (rstLink.Rows.Count > 0)
    {
        string jumpURL = rstLink.Rows[0]["URLLink"].ToString();
        Response.Redirect(jumpURL);
    }
}

因此,根据点击的链接按钮,我们获取data-page-id,从数据库中获取该值,然后跳转到该URL。

通过上述设计,无论页面上有1个还是20个链接,都不重要,因为在页面加载时,我们不进行任何数据库操作,只有当用户实际点击按钮时才会进行操作。

因此,我们不会在页面加载时产生任何数据库提取和操作,从而不会对服务器产生不必要的负载。

请注意,每个LinkButton都可以在代码后台使用/调用相同的例程。

英文:

Ok, so the goal is to have hyperlinks on a page, but the resolution of the link is to occur say from a database.

I would NOT replace all of the hyperlinks on page load, since this is too high of a database cost. Say we had 10 links on a page. If we replace the links at page load time, then that is 10 database operations. That is rather expensive, since say if we had 20 users on the site, then we done 200 database operations. That is simply WAY TOO many database hits and pulls. This is especially the case since the user will NOT YET have clicked on any of the links!

So, I suggest using and dropping into the page a SERVER SIDE hyper-link control.

Say, like this (2 example link buttons)

        &lt;asp:LinkButton ID=&quot;L1&quot; runat=&quot;server&quot;
            OnClick=&quot;mylinkresolve_Click&quot;
            data-page-id=&quot;12345&quot;                
            &gt;My Link Text&lt;/asp:LinkButton&gt;

        &lt;br /&gt;

        &lt;asp:LinkButton ID=&quot;L2&quot; runat=&quot;server&quot;
            OnClick=&quot;mylinkresolve_Click&quot;
            data-page-id=&quot;6789&quot;                
            &gt;My Link Text2 &lt;/asp:LinkButton&gt;

Now, you could consider using the ID in above, but either way, don't really matter. For above, we use data-page-id.

So, now in the code behind, we have ONE routine that ALL link buttons point to.

The code would be like this:

    protected void mylinkresolve_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        string strSQL = @&quot;SELECT Hlink FROM tblMyLinks 
                        WHERE LinkID = @LinkID&quot;;

        SqlCommand cmdSQL = new SqlCommand(strSQL);
        cmdSQL.Parameters.Add(&quot;@LinkID&quot;, SqlDbType.NVarChar).Value = btn.Attributes[&quot;data-page-id&quot;];

        DataTable rstLink = General.MyRstP(cmdSQL);
        if (rstLink.Rows.Count &gt; 0)
        {
            string jumpURL = rstLink.Rows[0][&quot;URLLink&quot;].ToString();
            Response.Redirect(jumpURL);
        }
    }

So, based on the link button clicked, we get the data-page-id, pull the value from the database and then jump to that URL.

With the above design, then it don't matter if we have 1, or 20 links on the page, since at page load time, we don't do ANY database operations, but ONLY when a user actually clicks on a button.

As a result, we don't incur any database pull and operations at page load time, and thus will not place unnecessary load on the server.

Note how each LinkButton can use/call the ONE SAME routine in code behind.

huangapple
  • 本文由 发表于 2023年6月13日 15:29:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定