残留中的回忆 通过本文主要向大家介绍了新闻发布 asp.net,新闻发布系统asp.net,asp.net,asp net培训,asp和asp.net的区别等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
大家都知道,生成静态页的方法有两种,第一种是使用C#在后台硬编码,第二种是读取模板文件,使用字符串代替。总体来讲第一种方法代码量比较大,维护起来有点困难。生成静态页的目的是为了提高用户体验度,加快访问速度。
使用静态页面还有如下好处:
1、 安全:使用静态页面,用户访问的使没有任何操作功能的html页面,可以说从安全性方面大大提高了程序及服务器的安全。
2、 快速:用户访问的是提前生成好的静态页面,使用户对页面的请求瓶颈只受IO的限制而不会有其他方面的影响。
3、 降低服务器,数据库负载:因为用户访问的是静态页,对承载静态页的服务器配置要求降低了许多,同时,不会因为过大的访问量,造成数据库服务器负载过重等问题。
下面分享asp.net新闻列表生成静态页之批量和单页生成。前期准备,需要新建一个文件夹。。前台展示:/new/default.aspx 这个页面放的就是动态的新闻列表数据。
先看下图:
不多说了上代码
这里要说明一下,我用的AspNetPager分页控件,需要设置一下
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" FirstPageText="首页" LastPageText="末页" NextPageText="下一页" NumericButtonCount="10" OnLoad="AspNetPager1_Load" OnPageChanged="AspNetPager1_PageChanged1" PageSize="13" PrevPageText="上一页" Font-Bold="False" Font-Size="13px" CssClass="badoo" UrlPaging="true" CurrentPageButtonPosition="Center" PagingButtonSpacing="5px" EnableUrlRewriting="True" UrlRewritePattern="news_{0}.html" ShowMoreButtons="False" ShowPageIndexBox="Never" > </webdiyer:AspNetPager></div>
需要设置三个地方:
UrlPaging="true" 设置启用url来传递分页信息 EnableUrlRewriting="True" 启用URL重写 UrlRewritePattern="news_{0}.html" 设置分页URL重写格式
Static.ashx页面代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using System.IO; using System.Net; using System.Data; namespace Web.admin.ashx { /// <summary> /// Static 生成静态页 /// </summary> public class Static : IHttpHandler { public void ProcessRequest(HttpContext context) { string url = context.Request["geturl"]; string path = context.Request["getpath"]; string id = context.Request["id"]; string act = context.Request["act"]; string page = context.Request["pages"]; string pagecount = context.Request["pagecouts"]; string table = context.Request["table"]; string str = ""; if (act == "0") { //生成当前选中项 string ids = id.Substring(0, (id.Length - 1)); string[] s_id = ids.Split(','); for (int i = 0; i < s_id.Length; i++) { string path2 = context.Server.MapPath("../../" + path + s_id[i] + ".html"); str = CreateHTML("http://localhost:4216/" + url + s_id[i], path2); //这里必须是拼接的绝对路径 string urls = "http://localhost:4216/" + path + s_id[i] + ".html"; //这里必须是拼接的绝对路径 string sql = string.Format("update {0} set Static =1,Url='{1}' where id={2}", table, urls, s_id[i]); if (new DBTool.DB().RunSqlGetRowCount(sql) > 0) { } } context.Response.Write(str); context.Response.End(); } if (act == "1") { //生成当前页列表 string path2 = context.Server.MapPath("../../" + path + page + ".html"); str = CreateHTML("http://localhost:4216/" + url + page, path2); context.Response.Write(str); context.Response.End(); } if (act == "2") { //生成全部列表 int count = Convert.ToInt32(pagecount); for (int i = 1; i <= count; i++) { string path2 = context.Server.MapPath("../../" + path + i + ".html"); str = CreateHTML("http://localhost:4216/" + url + i, path2); } context.Response.Write(str); context.Response.End(); } if (act == "3") { //生成全部项 string sqlsel =string.Format("select Id from {0}",table); DataTable dt = new DBTool.DB().RunSqlGetDataTable(sqlsel); if (dt.Rows.Count > 0) { string idall = ""; for (int i = 0; i < dt.Rows.Count; i++) { idall += dt.Rows[i]["Id"].ToString()+","; } string ids = idall.Substring(0, (idall.Length - 1)); string[] s_id = ids.Split(','); for (int ii = 0; ii < s_id.Length; ii++) { string path2 = context.Server.MapPath("../../" + path + s_id[ii] + ".html"); str = CreateHTML("http://localhost:4216/" + url + s_id[ii], path2); string urls = "http://localhost:4216/" + path + s_id[ii] + ".html"; string sql = string.Format("update {0} set Static =1,Url='{1}' where id={2}", table, urls, s_id[ii]); if (new DBTool.DB().RunSqlGetRowCount(sql) > 0) { } } context.Response.Write(str); context.Response.End(); } } } public string CreateHTML(string strurl, string path) { string str = ""; StreamReader sr; StreamWriter sw = null; try { WebRequest HttpWebRequest = WebRequest.Create(strurl); WebResponse HttpWebResponse = HttpWebRequest.GetResponse(); sr = new StreamReader(HttpWebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")); string strHtml = sr.ReadToEnd(); sw = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("utf-8")); sw.WriteLine(strHtml); sw.Flush(); sw.Close(); str = "生成成功!"; } catch (Exception ex) { str = "生成失败,失败原因" + ex.Message+"。"; } return str; } public bool IsReusable { get { return false; } } } }</div>
后台生成前台代码
<div> <table border="0" cellpadding="0" cellspacing="0" class="list_table1"> <tr> <td width="13%"><input type="text" runat="server" id="text_word" class="text lh20" placeholder="请输入关键字"/></td> <td width="5%"> <asp:Button ID="Button1" runat="server" Text="搜索" CssClass="suborange" OnClick="Button1_Click" /> </td> <td width="82%"> <a href="list.aspx" class="subgreen">查看全部</a></td> </tr> </table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="list_table"><tr> <th width="30"> </th> <th width="60" data-field="id" data-url="newslist.aspx" >ID<img id="lab_id" src="images/a2.png" /></th> <th width="200" data-field="Name" data-url="newslist.aspx" >名称<img id="lab_Name" src="images/a2.png" /></th> <th width="200" data-field="Dir" data-url="newslist.aspx">地址<img id="lab_Dir" src="images/a2.png" /></th> <th width="150" data-field="Static" data-url="newslist.aspx">静态化<img id="lab_Static" src="images/a2.png" /></th> <th>操作</th> </tr> <asp:Repeater ID="R1" runat="server" OnItemDataBound="R1_ItemDataBound" ><ItemTemplate> <tr class="tr" id='tr_<%#Eval("id")%>'> <td class="td_center"><label id='labe_<%#Eval("id")%>'><asp:CheckBox ID="chkItem" runat="server" ToolTip='<%#Eval("id")%>'></asp:CheckBox></label><input type="hidden" id="text_id" runat="server" value='<%#Eval("id")%>' /></td> <td class="td_center"> <%#Eval("id") %></td> <td class="td_center"> <%#Eval("Name") %></td> <td class="td_center"> <a href="javascript:;" class="a_1" data-id='&