【问题标题】:Best way to inject HTML into a page (server-side) in ASP.NET在 ASP.NET 中将 HTML 注入页面(服务器端)的最佳方法
【发布时间】:2011-06-20 13:07:06
【问题描述】:

我有一个返回 HTML 块的 HTTPHandler。将其注入服务器上的控件的最佳方法是什么?

我主要通过使用 asp:literal 并使用 WebClient.downloadString() 从处理程序中获取文本来工作

<asp:Literal runat="server" ID="Text_Page1" Visible="false"></asp:Literal>
<asp:Literal runat="server" ID="Text_Page2" Visible="false"></asp:Literal>

然后在服务器端方法中:

Text_Page1.Text = new WebClient().DownloadString("http://localhost:666/" +sPage1URL);
Text_Page2.Text = new WebClient().DownloadString("http://localhost:666/" +sPage2URL);

硬编码的网址目前仅用于测试。以前我尝试只使用“~/”+URL 来尝试构建它,但是 WebClient 库抛出了一个异常,说 URL 太长(我认为这不是真的)

关于从服务器端执行此操作的最佳方法有什么想法吗?

编辑:当我说“最佳”时,我的意思是最高效并遵守“最佳实践”。当我的方法放在经过身份验证的 IIS 上时,它的效果并不好。我在验证时遇到问题。我以为这样做了

WebClient oClient = new WebClient();
oClient.Credentials = CredentialCache.DefaultCredentials;
oClient.UseDefaultCredentials = true;
String sData = oClient.DownloadString(sURL); 

可以,但我收到 401 错误。有人有其他选择吗?

干杯

戈登

【问题讨论】:

  • 最好的方法是什么?表现?可维护性?可读性?还有什么?
  • 您要从远程站点或应用程序域获取 html 吗?还是您打算从同一个站点/应用程序中获取 HTML?
  • 性能+可维护性最佳,抱歉..
  • 我将从当前站点阅读

标签: asp.net .net-2.0 httphandler server-side


【解决方案1】:

在不询问有关通过 web 请求从提供包含内容的同一应用程序获取 html 背后的推理的任何问题的情况下,我会将功能包装在 WebUserControl 中。大致如下:

using System;
using System.Net;
using System.Web.UI;

public partial class HtmlFetcher : UserControl
{
    //configure this somewhere else in the real world, web.config maybe
    private const string ServiceUrl = "http://localhost:666/";

    public string HtmlPath { get; set; }

    protected override void Render(HtmlTextWriter output)
    {
        string outputText = String.Empty;
        try
        {
            outputText = new WebClient().DownloadString(string.Format("{0}{1}", ServiceUrl, HtmlPath));

        } catch (Exception e)
        {
            //Handle that error;
        }
        output.Write(outputText);
    }
}

这是您将其添加到页面的方式:

<%@ Register src="HtmlFetcher.ascx" tagname="HtmlFetcher" tagprefix="uc1" %>
<uc1:HtmlFetcher ID="HtmlFetcher1" HtmlPath="some-test-file.html" runat="server" />

【讨论】:

    【解决方案2】:

    您将在字符串 lcHtml 中获取数据,然后根据需要使用它

     // *** Establish the request
        string lcUrl = "http://yourURL";
    HttpWebRequest loHttp =
             (HttpWebRequest) WebRequest.Create(lcUrl);
    
        // *** Set properties
        loHttp.Timeout = 10000;     // 10 secs
        loHttp.UserAgent = "Code Sample Web Client";
    
        // *** Retrieve request info headers
        HttpWebResponse loWebResponse = (HttpWebResponse) loHttp.GetResponse();
    
        Encoding enc = Encoding.GetEncoding(1252);  // Windows default Code Page
    
        StreamReader loResponseStream =
           new StreamReader(loWebResponse.GetResponseStream(),enc);
    
        string lcHtml = loResponseStream.ReadToEnd();
    
        loWebResponse.Close();
        loResponseStream.Close();
    

    【讨论】:

    • 这与使用 WebClient 有何不同(除了代码更多)
    【解决方案3】:

    您可以使用&lt;% %&gt; 在您的aspx 中使用服务器端代码块。

    试试这个:

    <% new WebClient().DownloadString("http://localhost:666/" +sPage1URL) %>
    

    【讨论】:

    • 如果WebClient抛出异常,噗,页面就到这里了。
    猜你喜欢
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 2015-05-06
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    相关资源
    最近更新 更多