ASP. NET static note

Source: Internet
Author: User

I recently read some staticArticleSuch as static and pseudo-static methods. However, useful articles are scattered and the knowledge points are different. For developers with heavy operations like me, too many theories are annoying. The most important thing is to do this, is truth. I will not talk too much about all the theoretical things. I really want to look at the various static principles and recommend them to buy relevant books for a good study.

I have summarized all the static technologies I know, such as using the urlrewrite component, for example, rewriting the methods in httphandler ....

Method 1: first download a urlrewrite component, decompress the package, and introduce rewrite. DLL into the project. First, you write data in Web. config.

 
<Configsections> <section name = "rewriterconfig" type = "urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter "/> </configsections>  

Then, you need to configure your rewrite rules. Below are some of the rules configured in my previous project. For more information, it is not necessarily suitable for you.

<Rewriterconfig> <rules> <rewriterrule> <lookfor> ~ /Shwinfo/jurisdiction _ (\ W {3}) \. aspx </lookfor> <sendto> ~ /En/jurisdiction. aspx? Jurid = $1 </sendto> </rewriterrule> <lookfor> ~ /Articles/(\ D {1,}) \. htm </lookfor> <! -- This is the replaced file name and uses the Regular Expression --> <sendto> <! [CDATA [~ /En/article_view.aspx? Article_id = $1]> </sendto> <! -- This is a webpage to be replaced. It is generally a webpage with parameters following a question mark --> </rewriterrule> <lookfor> ~ /Articles/(\ D {1,}) _ (\ D {1,}) \. htms </lookfor> <sendto> <! [CDATA [~ /En/article_view.aspx? Article_id = $1 & page = $2]> </sendto> </rewriterrule> </rules> </rewriterconfig>

Why is my webpage path http: // localhost/en/article_view.aspx? If article_id = 2, it will be replaced by http: // localhost/en/article/1.htm.

After completing this, you also need to configure your IIS to change the executable file path of the ASPX page to HTML, and remove the s check file to check whether it exists.

The second method is the real static method, which overwrites the template page,CodeFor more information, see.

Show the main code of write. aspx on the page

<Body style = "width: 500px; text-align: center; "> <Form ID =" form1 "runat =" server "> <div> <Table width =" 750px "> <tr> <TD colspan =" 4 "> Article Information </TD> </tr> <TD style = "width: 100px; "> title </TD> <TD style =" text-align: Left "> <asp: textbox id =" name "runat =" server "> </ASP: textbox> </TD> </tr> <TD style = "width: 100px; Height: 191px;"> content </TD> <TD style = "height: 191px "> <asp: textbox id = "MSG" textmode = "multiline" columns = "50" runat = "server" rows = "10" Height = "182px" width = "631px"> </asp: textbox> </TD> </tr> <TD colspan = "4"> <asp: button id = "BTN" runat = "server" text = "add" onclick = "btn_click" style = "width: 40px "/> </TD> </tr> </table> </div> </form> </body>

then write. aspx. CS Code

Protected void btn_click (Object sender, eventargs e) {String title = This. name. text; string content = This. MSG. text; content = content. replace ("\ n", "<br/>"); string time = datetime. now. tostring ("yyyy-mm-dd hh: mm: SS"); string filename = datetime. now. tostring ("yyyy-mm-dd-hh-mm-SS") + ". htm "; outputhtml (title, content, time, filename," htmltemp.htm ");} private void outputhtml (string T1, string T2, string T3, string fname, string temphtml) {string htmlpath = string. format (@ "\ HTML \ {0}", fname); string htmltemp = file. readalltext (server. mappath (". ") + @" \ "+ temphtml, encoding. utf8); stringbuilder sbuilder = new stringbuilder (htmltemp); sbuilder. replace ("{addtitle}", T1); sbuilder. replace ("{addcontent}", T2); sbuilder. replace ("{addtime}", T3); streamwriter swriter = file. createtext (server. mappath (". ") + @" \ HTML \ "+ fname); swriter. writeline (sbuilder. tostring (); swriter. flush (); swriter. close (); response. redirect (htmlpath );}

Below is the template page htmltemp.htm

 
<Body style = "text-align: center;"> <Table cellpadding = "5" cellspacing = "0" width = "750px" border = "2"> <! -- This page is used for static webpage templates, display the added information --> <tr> <TD align = "center"> {addtitle} </TD> </tr> <TD align = "Left"> {addcontent }</TD> </tr> <TD align = "right"> published on: {addtime} </TD> </tr> </table> </body>

In this way, if you need to use it, you can optimize it and save the generated HTML path to the database. Nothing else.

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.