繁体   English   中英

Razor中的硬编码网址

[英]Hard-coded url in Razor

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

正如它在代码注释中所说的,URL可能是硬编码的。 我很好奇它是如何硬编码到一个RSS feed的,看代码似乎并不明显。 例如,如果我尝试将URL替换为“ 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)

 } 
}

我相信作者的意思是URL本身是硬编码的。 即,不是从数据库,配置文件,用户界面或类似内容中动态加载的,而是由代码本身编写的。 在实际的解决方案中,如果您不仅想要静态提要或静态提要列表中的RSS,通常希望它更具动态性。 但是,如果您在生产代码后就永远不需要更改url,那么对其进行硬编码就不会有问题。

无需亲自尝试代码,从一开始就可以使用返回实际RSS提要的任何URL。 只要提要在XML中具有项目节点,并且只要项目节点具有标题和描述即可。

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

这是硬编码,用于获取rss feed。 http://tdsb.on.ca/RSS/MediaRoom.xml变为空白,因为格式不匹配。 如果同时打开两个xml并进行比较,则会发现tdsb下的ìtem缺少description

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM