asp.net mvc Practice

Source: Internet
Author: User
Taobao Bookstore: http://shop35357269.taobao.com Alimama_pid= "mm_10096109_566393_892186"; Alimama_titlecolor= "0000FF"; Alimama_descolor = "000000"; Alimama_bgcolor= "FFFFFF"; Alimama_bordercolor= "E6e6e6"; Alimama_linkcolor= "008000"; Alimama_bottomcolor= "FFFFFF"; alimama_anglesize= "0"; alimama_bgpic= "0"; alimama_icon= "0"; Alimama_sizecode= "11"; alimama_width=760; alimama_height=90; alimama_type=2; Alimama_pid= "mm_10096109_566393_1843170"; Alimama_titlecolor= "0000FF"; Alimama_descolor = "000000"; Alimama_bgcolor= "FFFFFF"; Alimama_bordercolor= "E6e6e6"; Alimama_linkcolor= "008000"; Alimama_bottomcolor= "FFFFFF"; alimama_anglesize= "0"; alimama_bgpic= "0"; alimama_icon= "0"; Alimama_sizecode= "12"; alimama_width=468; alimama_height=60; alimama_type=2;



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 ;
}
}
alimama_pid= "mm_10096109_566393_2123463"; alimama_titlecolor= "0000FF"; alimama_descolor = "000000"; Alimama_bgcolor= "FFFFFF"; Alimama_bordercolor= "E6e6e6"; Alimama_linkcolor= "008000"; Alimama_bottomcolor= "FFFFFF"; alimama_anglesize= "0"; alimama_bgpic= "0"; alimama_icon= "0"; Alimama_sizecode= "12"; alimama_width=468; alimama_height=60; alimama_type=2; var alimama_pid= "mm_10096109_566393_892989"; var alimama_titlecolor= "0000FF"; var alimama_descolor = "000000"; var Alimama_bgcolor= "FFFFFF"; var alimama_bordercolor= "E6e6e6"; var alimama_linkcolor= "008000"; var alimama_bottomcolor= "FFFFFF"; var alimama_anglesize= "0"; var alimama_bgpic= "0"; var alimama_icon= "0"; var alimama_sizecode= "11"; var alimama_width=760; var alimama_height=90; var alimama_type=2;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.