![](https://images.cnblogs.com/cnblogs_com/sonven/wap_moni_tool_view.jpg)
注:通过目录下的/moni可以模拟浏览我们制作好的wap网站
我们先实现一个Page类,添加一些于aspx页的交互,因为wap可能不支持viewState吧
Page.cs 注意与System.Web.UI.Page分开
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Rsion.Web
{
public abstract class Page : System.Web.UI.Page
{
private TempDatas<string, Object> tempData;
public Page() { BindEvents(); }
/// <summary>
/// 页面临时数据
/// </summary>
public TempDatas<String, Object> TempData
{
get
{
if (tempData == null) tempData = new TempDatas<string, Object>();
return tempData;
}
}
public PageAdapter Html
{
get { return new PageAdapter(this); }
}
/// <summary>
/// 绑定事件
/// </summary>
protected virtual void BindEvents()
{
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Rsion.Web;
namespace Rsion.Web
{
public abstract class Application:System.Web.HttpApplication
{
public static Template Template;
/// <summary>
/// 模板缓存时间
/// </summary>
public static int TemplateCacheTime = 10;
/// <summary>
/// 重启Web进程
/// </summary>
public static void RestartWebProcess()
{
HttpRuntime.UnloadAppDomain();
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
// Author : Sonven
// Blog : Sonven.cnblogs.com
namespace Rsion.Web
{
public class TempDatas<TKey,TValue>:CollectionBase
{
private Dictionary<TKey, TValue> dataArray;
public TValue this[TKey key]
{
get
{
if (dataArray.ContainsKey(key))return dataArray[key];
throw new ArgumentException("未添加此数据项进入该集合!", "TKey", null);
}
set
{
dataArray = dataArray ?? new Dictionary<TKey, TValue>();
if (dataArray.ContainsKey(key)) dataArray[key] = value;
else dataArray.Add(key, value);
}
}
/// <summary>
/// 添加一个键值数据
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void Add(TKey key, TValue value)
{
dataArray=dataArray??new Dictionary<TKey,TValue>();
dataArray.Add(key,value);
}
}
}
我们扩展Page类创建一个PageAdapter.cs (用于添加模板支持)
(rsion.com,锐讯,巴中广州佛山成都网站建设,newmin,new.min,new.min@msn.com,newmin.net,巴中网站建设tel:18608275575锐讯)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
namespace Rsion.Web
{
/// <summary>
/// WebPage页面辅助适配器类
/// </summary>
public class PageAdapter
{
private Page page;
public PageAdapter(Page page)
{
this.page = page;
}
/// <summary>
/// 显示模板
(rsion.com,锐讯,巴中广州佛山成都网站建设,newmin,new.min,new.min@msn.com,newmin.net,巴中网站建设tel:18608275575锐讯)
/// </summary>
/// <param name="partialPath">模板文件路径:不带后缀[模板后缀.tpl]如/bottom将显示Templates下的bottom.tpl文件</param>
public void RenderPartial(string partialPath)
{
string templateID="Template_"+partialPath.Replace("/", "_");
object o = HttpRuntime.Cache[templateID];
if (o == null)
{
FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath("~/templates/" + partialPath + ".tpl"));
if (!fi.Exists) return;
string templateContent;
using (StreamReader sr = new StreamReader(fi.FullName))
{
templateContent = sr.ReadToEnd();
}
//转换
TransformTemplateTags(ref templateContent);
//写入缓冲
HttpRuntime.Cache.Insert(templateID, templateContent, null,
DateTime.Now.AddMinutes(Application.TemplateCacheTime),TimeSpan.Zero);
HttpContext.Current.Response.Write(templateContent);
}
else
HttpContext.Current.Response.Write(o.ToString());
}
/// <summary>
/// 转换模板内容
/// </summary>
/// <param name="templateContent"></param>
private void TransformTemplateTags(ref string templateContent)
{
string templateID;
string pattern=@"\${(\w+)}";
Regex rg = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
foreach(Match m in rg.Matches(templateContent))
{
templateID = Regex.Replace(m.Captures[0].Value, pattern, "$1");
templateContent = Regex.Replace(templateContent, @"\${" + templateID + "}",
Application.Template.Rules[templateID].ToString());
}
}
/// <summary>
/// 转换该页的标签内容
(rsion.com,锐讯,巴中广州佛山成都网站建设,newmin,new.min,new.min@msn.com,newmin.net,巴中网站建设tel:18608275575锐讯)
/// </summary>
public void TransformPageTags()
{
///
///TO:DO
![](https://www.cnblogs.com/Images/dot.gif)
///
}
}
}
现在我们要实现可以用于wap的page类了,WapPage.cs
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.IO;
// Author : Sonven
// Blog : Sonven.cnblogs.com
namespace Rsion.Web
{
public class WapPage:Page
{
public WapPage() : base() { }
/// <summary>
/// 绑定事件
/// </summary>
protected override void BindEvents()
{
Page.Load += delegate(object s, EventArgs e)
{
HttpContext.Current.Response.Write("<?xml version=\"1.0\"?>\r" +
"<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\">\r");
};
Page.LoadComplete += delegate(object s, EventArgs e)
{
HttpContext.Current.Response.ContentType = "text/vnd.Web.wml";
};
//处理错误时候转向错误页面[仅在发布后]
#if DEBUG
#else
Page.Error += delegate(object s, EventArgs e)
{
Session["errormsg"] = HttpContext.Current.Error.Message + "<br />" +
"地址:" + HttpContext.Current.Request.RawUrl.ToString();
HttpContext.Current.Response.Redirect("~/error.aspx");
};
#endif
Page.PreRender += delegate(object s, EventArgs e)
{
};
}
}
}
这样就差不多只要继承WapPage就可以实现wap网页开发了
接下来我们创建模板,并给模板加上缓存提高性能
Application.cs用于提供缓存时间
(rsion.com,锐讯,巴中广州佛山成都网站建设,newmin,new.min,new.min@msn.com,newmin.net,巴中网站建设tel:18608275575锐讯)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Rsion.Web;
namespace Rsion.Web
{
public abstract class Application:System.Web.HttpApplication
{
public static Template Template;
/// <summary>
/// 模板缓存时间
/// </summary>
public static int TemplateCacheTime = 10;
/// <summary>
/// 重启Web进程
/// </summary>
public static void RestartWebProcess()
{
HttpRuntime.UnloadAppDomain();
}
}
}
接下来我们创建一个单独的Template项目先
在里面创建Template.cs,ParamRules
ParamRules实现如下:
(rsion.com,锐讯,巴中广州佛山成都网站建设,newmin,new.min,new.min@msn.com,newmin.net,巴中网站建设tel:18608275575锐讯)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Rsion.Web
{
/// <summary>
/// 模板参数规则类
/// </summary>
public class ParamRules:IEnumerable
{
private Dictionary<string, object> rules;
public ParamRules()
{
if (rules == null) rules = new Dictionary<string, object>();
}
public object this[string paramKey]
{
get
{
if (rules.ContainsKey(paramKey)) return rules[paramKey];
return "";
}
set
{
if (rules.ContainsKey(paramKey)) rules[paramKey] = value;
else rules.Add(paramKey, value);
}
}
/// <summary>
/// 添加新的规则
/// </summary>
/// <param name="paramKey"></param>
/// <param name="paramValue"></param>
public void Add(string paramKey, object paramValue)
{
if (rules.ContainsKey(paramKey))
throw new ArgumentException("对不起规则已经存在!Key:" + paramKey + ",Value:" + rules[paramKey].ToString(), "paramKey");
rules.Add(paramKey, paramValue);
}
public void Remove(string paramKey, object paramValue)
{
if (rules.ContainsKey(paramKey))
rules.Remove(paramKey);
}
#region IEnumerable 成员
public IEnumerator GetEnumerator()
{
foreach (KeyValuePair<string, object> k in rules)
{
yield return k;
}
}
#endregion
}
}
Template.cs实现如下:
(rsion.com,锐讯,巴中广州佛山成都网站建设,newmin,new.min,new.min@msn.com,newmin.net,巴中网站建设tel:18608275575锐讯)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Rsion.Web
{
/// <summary>
/// 模板
/// </summary>
public class Template
{
public static Template _template;
private static ParamRules rules;
private Template() { }
public static Template CreateInstance()
{
if (_template == null) _template = new Template();
return _template;
}
public ParamRules Rules
{
get
{
if (rules == null) rules = new ParamRules();
return rules;
}
}
}
}
这样我们先在global.asax中填加一些模板数据,这样才可以解析模板,解析模板的功能实现在PageAdapter中,这样可以
在本页面直接调用Html.RenderPartial("template")调用
gobal.asax
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Rsion.Web;
using c=Rsion.Web.Config;
namespace Rsion.Wap
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
InitTemplate();//初始化模板数据,只针对那些不经常变化的数据
}
# region events
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
#endregion
private void InitTemplate()
{
global::Rsion.Web.Template t = Template.CreateInstance();
//添加模板数据规则,只用于不常更新的数据 如key=webname 则{$webname}替换成value
t.Rules.Add("webname",c.Web.Current.WebName);
t.Rules.Add("weburi", c.Web.Current.WebUri);
t.Rules.Add("sysname", "sonven's wap develop framework!");
Rsion.Web.Application.Template = t;
//模板缓存过期时间(分钟)(默认10分钟)
Rsion.Web.Application.TemplateCacheTime = 0;
}
}
}
(rsion.com,锐讯,巴中广州佛山成都网站建设,newmin,new.min,new.min@msn.com,newmin.net,巴中网站建设tel:18608275575锐讯)
然后着手开发wap项目了
首先新建一个default.aspx,default.aspx.cs
两文件如下
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rsion.Web.Config;
using Rsion.Web;
namespace Rsion.Wap
{
public partial class Default:WapPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TempData.Add("webname", Gobal.Web.WebName);
TempData.Add("webUri", Gobal.Web.WebUri);
}
}
}
}
default.aspx
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Rsion.Wap.Default" %>
<wml>
<head></head>
<card title="<%=TempData["webname"] %>">
<%Html.RenderPartial("top"); %>
<%Html.RenderPartial("index"); %>
数据绑定用<%=TempData[id] %><br /><br />调用显示模板<% Html.RenderPartial("<br />模板在Templates下的路径不包括.tpl)<br />
模板中用:${templateId}代替符号<br />
然后在使用Rsion.Web.Application.<br />Template.Rules.Add(templateID,value)<br />
就可以调出value值了!
<%Html.RenderPartial("bottom"); %>
</card>
</wml>
怎么样呢是不是很简单,接着创建模板
文件放在/Templates/下哦,文件扩展为.tpl
bottom.tpl
<a href="/">首页</a> |
<a href="http://www.cnyolee.com">有理网</a> |
<a href="http://sonven.cnblogs.com">博客园</a> |
<a href="http://www.rsion.com">联系我</a>
<br />
${webname} ${weburi}
同理创建其他的模板
我们在Page类里面实现了友好的自定义错误页,我们创建显示这个页面的error.aspx
error.aspx
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Error.aspx.cs" Inherits="Rsion.Wap.Error" %>
<wml>
<card title="对不起出错了!">
手机锐讯网 Web.rsion.com <br />
错误信息:<br />
<%=TempData["errormsg"]%>
</card>
</wml>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rsion.Web;
namespace Rsion.Wap
{
public partial class Error:WapPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.TempData["errormsg"] = Session["errormsg"] ?? "系统执行出错!";
}
}
}
}
Ok了接下来就该验收结果了,达开/moni在里面输入你的地址就可以看到wap已经可以正常在浏览器中显示了
关于模拟器的实现请看我的另篇文章
点此下载该项目代码