剃刀中的硬编码网址

时间:2012-11-22 19:38:47

标签: c# xml razor

在C#中寻找一个简单的RSS阅读器用于剃刀语法,我偶然发现了这个网站:http://our.umbraco.org/forum/developers/razor/27409-Consume-an-RSS-feed-in-Razor

正如代码中的评论所述,它可能是URL硬编码。我很好奇它是如何硬编码到一个RSS提要的,看起来代码似乎并不明显。例如,如果我尝试替换“http://tdsb.on.ca/RSS/MediaRoom.xml”的URL,它就会变成空白。

@using System.Xml;

@{
//Get the XML from remote URL
XmlDocument xml = new XmlDocument();

**//URL currently hardcoded - but you could use a macro param to pass in URL**
xml.Load("http://blog.orcare.com/rss");

//Select the nodes we want to loop through
XmlNodeList nodes = xml.SelectNodes("//item");

//Traverse the entire XML nodes.
foreach (XmlNode node in nodes)
{
    //Get the value from the <title> node
    var title = node.SelectSingleNode("title").InnerText;

    //Get the value from the <description> node
    var description = node.SelectSingleNode("description").InnerText;

    <h1>@title</h1>
    @Html.Raw(description)

 } 
}

2 个答案:

答案 0 :(得分:0)

我相信作者的意思是URL本身是硬编码的;即,不是从数据库,配置文件,用户界面或类似物动态加载,而是写在代码本身。在真正的解决方案中,如果您不仅希望从静态订阅源或静态订阅源列表中获取RSS,那么您通常会希望这更具动态性。但是,如果您在使用代码进行生产后永远不需要更改URL,那么它就不是硬编码的问题。

我没有亲自尝试过代码,从第一印象开始,它应该与任何返回实际RSS提要的网址一起使用。只要Feed中有XML项目节点,并且只要项目节点具有标题和描述。

但代码不是很扎实。如果XML中缺少这些元素中的任何一个,它就很容易中断。

答案 1 :(得分:0)

它是硬编码获取rss源的地方。 http://tdsb.on.ca/RSS/MediaRoom.xml变为空白,因为格式不匹配。如果您同时打开xmls并进行比较,则会在tdsb下找不到description ìtem