Google_ad_client = "pub-6430022987645146"; Google_ad_slot = "8067578699"; Google_ad_width = 250; Google_ad_height = 250;
Rssin theWebfairly common in the system,mainly used to quickly browse the site update articles and so on content,isweb2.0one of the main characteristics of,how we used to make itRssWhat's the input??in theaspxoutput in?Customhttphandle?naturally it can be,but here it is.MVCin the framework,we can choose a better way,CustomActionResult.
according to official information,eachActionare returned to aActionResultto performView,andActionResultis an abstract class,all we have to do now is customize arssaction. first, you need to build aArticleresultdemoof theasp.net Web application.and then according toMVCconventions to establish related folders and files,in order to achieveRSSOutput,I add the following file,as shown in figure:
in theModelsin, ArticleentityisArticlecounterpart Entities, ArticlemodelThere is a test method for returning a set ofarticleentity,entityextensionsis theEntityprovides a set of extension methods,For example BuildRSSand other functions, Rssentityis to provideRSSData Entities.in theControllersin, Rssresultis that we have expandedActionresult,democontrolleris an extension ofControllerclass,provide a quickRssOutput Method,This is an abstract class, Articlecontrolleris the currentDemothe main control class.
about the fewEntityclass to describe the, Rssentitythe file containsrssentity,rssimage,rssitem3a class,for the entireRSSthe data is encapsulated. Entityextersionsclass provides a set of extension methods to implement an entity-rss XMLConversion of Data,specificentityextersionsthe code below:
PublicStaticstringtoxmlstring ( ThisRssItem Item) { StringBuilder sb = new StringBuilder (); Sb. Appendline ("<item>"); Sb. Append (Toxmlitem<RssItem>(item)); Sb. Appendline ("</item>"); return sb. ToString (); }
PublicStaticstringtoxmlstring ( Thisrssimage image) { StringBuilder sb = new StringBuilder (); Sb. Appendline ("<image>"); Sb. Append (Toxmlitem<rssimage>(image)); Sb. Appendline ("</image>"); return sb. ToString (); }
PublicStaticstringtoxmlstring ( Thisrssentity RSS) { StringBuilder SB=NewStringBuilder (); Sb. Appendline ("<?xml version="1.0"encoding="UTF-8"?>"); Sb. Appendline ("<rss version="2.0">"); Sb. Appendline ("<channel>"); Sb. Appendline (Toxmlitem<rssentity>(RSS)); Sb. Appendline ("</channel>"); Sb. Appendline ("</rss>"); returnsb. ToString (); }
PublicStaticrssentity Todefaultrss ( ThisIList<articleentity>articlelist) { Rssentity RSS=Newrssentity () { Title="articleresult Demo Rss.", Copyright="Copyright 2008 Leven", Generator="Articleresult Demo", Link="http://blog.leven.com.cn/", Description="Articleresult Demo rss-a demo of asp.net mvc priview3.", Webmaster="Leven", Image=NewRssimage () { Link="yun_qi_img/logo.jpg", Title="Articleresult Demo", URL="http://blog.leven.com.cn/", Description="articleresult Demo Image." } }; foreach(articleentity article inArticlelist) { Rss. Items.Add (NewRssItem () { Title=article. Title, Author=Article. Postuser, Category="Default Category", Link="http://blog.leven.com.cn/", Guid="http://blog.leven.com.cn/", Pubdata=article. Posttime, Description=article. Content }); } returnRSS; }
Private StaticstringToxmlitem<Dtype>(Dtype data) whereDtype:class { StringBuilder SB=NewStringBuilder (); Type type=Data. GetType (); Propertyinfo[] PiS=type. GetProperties (); foreach(PropertyInfo P inPiS) { if(P.propertytype==typeof(DateTime)) { Sb. AppendFormat ("<{0}>{1:r}</{0}>rn", P.name.tolower (), P.getvalue ( data, null)); } Elseif(P.propertytype== typeof(Rssimage)) { Sb. Appendline ((rssimage) p.getvalue (data, null). Toxmlstring ()); } Elseif(P.propertytype==typeof(IList<RssItem>)) { IList<RssItem>Rssitems=p.getvalue (data,NULL) asIList<RssItem>; foreach(RssItem Item inRssitems) { Sb. Appendline (item. Toxmlstring ()); } } Else { Sb. AppendFormat ("<{0}><![ Cdata[{1}]]></{0}>rn", P.name.tolower (), P.getvalue (data, null) ); } } returnsb. ToString (); }
through these methods,we can easily generateRSSData.
Look againRssresultclass.This class inherits from theActionResultclass,realized theExecuteresultMethod.This method is: Executeresult (controllercontext context)in which we can directlyRSSData Output.this isActionResultthe charm of the,we passedrssentity+rssactionFull to entity-xmlthe output is encapsulated,So that the program can be very convenient to implementRSSOutput.now give theRssresultthe Code:
since these twoRssmethod is notAction,so add the[Nonaction]of theAttubite.
now it's very convenient for us to use it again.,in theArticlecontrollerin,Implement aRssmethod
a direct output.RSS.Last modifiedWeb.config,AddRouteafter the completion of the,The implementation diagram is as follows:
explain,in thePRIVIEW3in the official statement,to make the default home page available,can add aDefault.aspxfile,then add a line to the page
<%Response.Redirect ("Article/rss")%>
I see a friend who's questioning this grammatical error.,no semicolon added,actually this is. NETproblems with the default language,If you do not modify. NETthe configuration,defaultaspxthe language isvb.netof the,so there's no problem with this line..
Finally, it gives theDemodownload of all engineering documents.
Click to download the project file
Personal blog Sync Update: http://blog.leven.com.cn/Article_28.aspx
PublicActionResult Rss () { Rssentity rss = articlemodel.getlist (). Todefaultrss (); return RSS (RSS); }
PublicEncoding contentencoding { get; Set ; }
Publicrssentity Data { get; Set ; }
PublicRssresult () { }
PublicRssresult (Encoding encode) { ContentEncoding = encode; }
PublicRssresult (rssentity data, Encoding encode) { Data = data; ContentEncoding = encode; }
PublicOverridevoidExecuteresult (ControllerContext context) { if( context==NULL) { throw new ArgumentNullException ("context" ); } Httpresponsebase Response=Context . Httpcontext.response; Response. ContentType="Text/xml"; if(contentencoding!=NULL) { Response. ContentEncoding = contentencoding; } if(Data!=NULL) { Response. Write (Data.toxmlstring ()); } }
to make it easier to use thisRssresult,we can treatControllerfor further rewriting.,I'll refer to it here.Jsonmethod is implemented by the wayDemocontroller,The code is as follows:
PublicAbstract classDemocontroller:controller { [Nonaction] Publicactionresult RSS (rssentity rss, Encoding encode) { Rssresult Result = new rssresult (rss, encode); return result ; }
[Nonaction] Publicactionresult RSS (rssentity RSS) { Rssresult Result = new Rssresult (); Result. Data = rss; return result ; } } |
|