服务器端包含外部 HTML?

发布于 2024-08-06 09:21:01 字数 205 浏览 2 评论 0原文

在我的 asp.net-mvc 应用程序中,我需要包含一个显示旧页面的页面。 该页面的主体是通过调用现有 Perl 脚本创建的。 此 Perl 脚本是外部托管的。

有没有办法做这样的事情:

<!-- #Include virtual="http://www.example.com/theScript.plx"-->

In my asp.net-mvc application I need to include a page that shows a legacy page.
The body of this page is created by calling an existing Perl script.
This Perl script is externally hosted.

Is there a way to do something like this:

<!-- #Include virtual="http://www.example.com/theScript.plx"-->

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟织青萝梦 2024-08-13 09:21:02

不作为直接包含,因为 ASP.NET 服务器端-includes 要求页面在服务器上编译。

您可以使用 jQuery 在页面加载时从该 URL 下载 HTML,尽管我知道这并不完美。

或者(我不知道这是否有效)您可以执行 WebRequest< /a> 从 ASP.NET MVC 控制器访问 perl 网页,并将生成的 HTML 作为文本放入视图中。这样你就可以利用诸如 之类的东西输出缓存以限制对 perl 页面的点击(如果它不经常更改)。

Not as a direct include, because ASP.NET server-side-includes require the page to be compiled at the server.

You could use jQuery to download the HTML from that URL when the page loads, though I appreciate that's not perfect.

Alternatively (and I have no idea whether this will work) you could perform a WebRequest to the perl webpage from your ASP.NET MVC controller, and put the resulting HTML in the view as text. That way you could make use of things like output caching to limit the hits to the perl page if it doesn't change often.

永不分离 2024-08-13 09:21:02

如果您想一次性完成所有操作,您可以从服务器发出 HTTP 请求并将内容写入页面吗?

像这样的事情:

Response.Write(GetHtmlPage("http://www.example.com/theScript.plx"));

调用此方法:(

public String GetHtmlPage(string strURL)
{
    // the html retrieved from the page
    String strResult;
    WebResponse objResponse;
    WebRequest objRequest = System.Net.HttpWebRequest.Create(strURL);
    objResponse = objRequest.GetResponse();
    // the using keyword will automatically dispose the object 
    // once complete
    using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
    {
        strResult = sr.ReadToEnd();
        // Close and clean up the StreamReader
        sr.Close();
    }
    return strResult;
}

大多数代码公然从 此处 因此未检查)

If you wanted to do it all in one go, you could do an HTTP Request from the server and write the contents to the page?

Something like this:

Response.Write(GetHtmlPage("http://www.example.com/theScript.plx"));

Calling this method:

public String GetHtmlPage(string strURL)
{
    // the html retrieved from the page
    String strResult;
    WebResponse objResponse;
    WebRequest objRequest = System.Net.HttpWebRequest.Create(strURL);
    objResponse = objRequest.GetResponse();
    // the using keyword will automatically dispose the object 
    // once complete
    using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
    {
        strResult = sr.ReadToEnd();
        // Close and clean up the StreamReader
        sr.Close();
    }
    return strResult;
}

(Most code ripped blatantly from here and therefore not checked)

风筝有风,海豚有海 2024-08-13 09:21:02

您可以通过简单地使用框架并将框架源设置为需要包含的 url 来以低调的方式实现此目的。这非常简单,无需任何服务器或客户端脚本即可关闭,因此如果可能的话,这将是我的首选方法。

但是,如果您希望 html 显示来自您的服务器,则需要手动包含它 - 通常通过使用 WebRequest (如 Neil 所说)。不过,您可能希望缓存远程页面以提高性能,因为它是一个 Perl 脚本,我假设该页面是动态的,所以这可能不是一个好主意。

You could implement this in a low-key fashion by simply using a frame and setting the frame source to the url that needs to be included. This is quite simple and can be down without any server or client side scripting, so that'd be my preferred approach, if possible.

If you want the html to appear to come from your server, however, you'll need to manually include it - typically by using WebRequest as Neil says. You may wish to cache the remote page for performance, though, since it's a perl script, I'll assume the page is dynamic, so this might not be a great idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文