C# 动态html新闻页在C中不起作用
我正在尝试创建一个动态新闻html页面。 当我尝试动态创建html时,问题就出现了。我是C新手,不知道怎么了。这是我的c代码:C# 动态html新闻页在C中不起作用,c#,web-scraping,C#,Web Scraping,我正在尝试创建一个动态新闻html页面。 当我尝试动态创建html时,问题就出现了。我是C新手,不知道怎么了。这是我的c代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using NewsAPI; using NewsAPI.Models; using News
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NewsAPI;
using NewsAPI.Models;
using NewsAPI.Constants;
namespace NewsDemo
{
public partial class Default1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
LoadNewsPage();
}
}
protected void LoadNewsPage()
{
try
{
var newsApiClient = new NewsApiClient("key");
List<string> mylist = new List<string>();
mylist.Add("google-news");
mylist.Add("bbc-news");
mylist.Add("cnn");
mylist.Add("the-new-york-times");
var articlesResponse = newsApiClient.GetTopHeadlines(new TopHeadlinesRequest
{
Sources = mylist,
Language = Languages.EN
});
if (articlesResponse.Status == Statuses.Ok)
{
string resulHtml = "";
resulHtml += "<table>";
foreach (var article in articlesResponse.Articles)
{
resulHtml += string.Format("<tr style='valign:top'>", "");
resulHtml += string.Format("<td width=20%><img src='{0}' width='250px' height='200px'></img></td>", article.UrlToImage);
resulHtml += string.Format("<td width=80%><a href='{0}'><h3>{1}</h3></a>{1}<br>{2}<br>{3}<br><br></td>", article.Url, article.Title, article.Author, article.Description);
resulHtml += string.Format("</tr>", "");
}
resulHtml += string.Format("</table>", "");
Label1.Text = resulHtml;
}
}
catch (Exception ex)
{
throw;
}
}
}
}
每当我尝试运行它时,在VS中,Chrome就会打开,页面永远不会加载。我不知道出了什么问题,因为我有一个控制台应用程序,可以获取新闻,工作正常
谢谢你的帮助。
谢谢 编辑
你对我答案的评论有助于我的理解。
我认为您的问题不取决于您的代码,而是取决于与Chrome+Visual Studio的配合
简言之:
要么你可以
还是你
我测试了你的LoadNewsPage,它的HTML格式很好。
奇怪的是,您正在将HTML插入标签的文本中
不要这样做,而是在标记中添加以下HTML:
<div id="MyNewsSection" runat="server"></div>
与
此外,作为一般调试帮助,在Chrome中按F12键:如果有任何错误,它们将显示给您。您是否尝试过调试它?是的,我尝试过如果您在受保护的无效页面后的左括号中放置断点,加载对象发送器,EventArgs e,它是否曾经被击中?我不认为它击中了?我怎么知道?我现在不在电脑旁。1小时后会回来的太好了!我现在不在家,但我到那里后会测试。但我仍然有同样的问题。页面拒绝加载当你在chrome中按f12时会发生什么?你能发布一个截图吗?好的,所以页面保持这样。我已经离开它至少一个小时之前,它仍然没有完成加载。当我按F12键时,我得到这个,还有这个
Label1.Text = resulHtml;
MyNewsSection.InnerHtml = resulHtml;