The parameters for the GetHtml method are worth noting too. This is typical of helper methods, and other utility methods in general. This means we do not have to create an instance of RssReader in order to call the GetHtml method from a web page or other code. You should also note that both the RssReader class and the GetHtml method are static. also makes TagBuilder available in a short-handed fashion too. The fully qualified reference for HtmlString is, and without adding a using statement to make available to the class file, we would have to type the fully qualified name every time we wanted to reference HtmlString. You can see public static HtmlString near the beginning of the code. The first 5 lines make certain namespaces available to the code so that when we reference items within them, we do not have to use their fully qualified name. Right - before you pass out, let me explain how this code works. Return new HtmlString(divTag.ToString()) PTag2.InnerHtml = dateValue.ToString( "G") Link = a10 != null ? feed.Element(a10 + "link").Attribute( "href").Value : feed.Element( "link").Value,ĭescription = feed.Element( "description")ĭivTag.MergeAttribute( "style", "width:" + width + "px") Īnchor.MergeAttribute( "href", item.Link) Var items = doc.Descendants( "item").Select(feed => new TagBuilder anchor = new TagBuilder( "a") TagBuilder divTag = new TagBuilder( "div") Public static HtmlString GetHtml( string url, int numberOfItems = 5,īool showDescription = true, bool showDateCreated = true, int width = 300) The type of file is a C# class file.Ĭall this file "RssReader.cs", and then remove all the code that's in it, then replace it with the following: using System ASP.NET will compile anything inside this the first time the site is run, and it will be available to other code throughout the site. Future versions of WebMatrix may offer this in something like an "Add New ASP.NET Folder" menu option at some stage, but that's not currently available. The first thing to do is to add a new folder to the web site. For that reason, I won't delve too deeply into the actual helper code, but I will draw attention to the key areas that should be considered when developing helpers. It lacks far too much exception handling for one thing, and I felt that adding too much would obscure the main thrust of this article. Its purpose is to illustrate the main points involved in creating a helper, and not to be a production-ready example of how to produce an Rss reader. The code that I will show here does the job, although it is far from perfect. So I am going to build a helper that allows the user to display part of an RSS feed on a web page. One area where there is no helper currently is consuming RSS feeds.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |