注:通过目录下的/moni可以模拟浏览我们制作好的wap网站
我们先实现一个Page类,添加一些于aspx页的交互,因为wap可能不支持viewState吧
Page.cs 注意与System.Web.UI.Page分开
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceRsion.Web
{
publicabstractclassPage:System.Web.UI.Page
{
privateTempDatas<string,Object>tempData;
publicPage(){BindEvents();}
///<summary>///页面临时数据
///</summary>publicTempDatas<String,Object>TempData
{
get
{
if(tempData==null)tempData=newTempDatas<string,Object>();
returntempData;
}
}
publicPageAdapterHtml
{
get{returnnewPageAdapter(this);}
}
///<summary>///绑定事件
///</summary>protectedvirtualvoidBindEvents()
{
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Web;
usingRsion.Web;
namespaceRsion.Web
{
publicabstractclassApplication:System.Web.HttpApplication
{
publicstaticTemplateTemplate;
///<summary>///模板缓存时间
///</summary>publicstaticintTemplateCacheTime=10;
///<summary>///重启Web进程
///</summary>publicstaticvoidRestartWebProcess()
{
HttpRuntime.UnloadAppDomain();
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Collections;
//Author:Sonven
//Blog:Sonven.cnblogs.comnamespaceRsion.Web
{
publicclassTempDatas<TKey,TValue>:CollectionBase
{
privateDictionary<TKey,TValue>dataArray;
publicTValuethis[TKeykey]
{
get
{
if(dataArray.ContainsKey(key))returndataArray[key];
thrownewArgumentException("未添加此数据项进入该集合!","TKey",null);
}
set
{
dataArray=dataArray??newDictionary<TKey,TValue>();
if(dataArray.ContainsKey(key))dataArray[key]=value;
elsedataArray.Add(key,value);
}
}
///<summary>///添加一个键值数据
///</summary>///<paramname="key"></param>///<paramname="value"></param>publicvoidAdd(TKeykey,TValuevalue)
{
dataArray=dataArray??newDictionary<TKey,TValue>();
dataArray.Add(key,value);
}
}
}
我们扩展Page类创建一个PageAdapter.cs (用于添加模板支持)
(rsion.com,锐讯,巴中广州佛山成都网站建设,newmin,new.min,new.min@msn.com,newmin.net,巴中网站建设tel:18608275575锐讯)
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Web.UI;
usingSystem.Web;
usingSystem.IO;
usingSystem.Text.RegularExpressions;
namespaceRsion.Web
{
///<summary>///WebPage页面辅助适配器类
///</summary>publicclassPageAdapter
{
privatePagepage;
publicPageAdapter(Pagepage)
{
this.page=page;
}
///<summary>///显示模板
(rsion.com,锐讯,巴中广州佛山成都网站建设,newmin,new.min,new.min@msn.com,newmin.net,巴中网站建设tel:18608275575锐讯)
///</summary>///<paramname="partialPath">模板文件路径:不带后缀[模板后缀.tpl]如/bottom将显示Templates下的bottom.tpl文件</param>publicvoidRenderPartial(stringpartialPath)
{
stringtemplateID="Template_"+partialPath.Replace("/","_");
objecto=HttpRuntime.Cache[templateID];
if(o==null)
{
FileInfofi=newFileInfo(HttpContext.Current.Server.MapPath("~/templates/"+partialPath+".tpl"));
if(!fi.Exists)return;
stringtemplateContent;
using(StreamReadersr=newStreamReader(fi.FullName))
{
templateContent=sr.ReadToEnd();
}
//转换TransformTemplateTags(reftemplateContent);
//写入缓冲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>///<paramname="templateContent"></param>privatevoidTransformTemplateTags(refstringtemplateContent)
{
stringtemplateID;
stringpattern=@"\${(\w+)}";
Regexrg=newRegex(pattern,RegexOptions.IgnoreCase|RegexOptions.IgnorePatternWhitespace);
foreach(Matchminrg.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>publicvoidTransformPageTags()
{
//////TO:DO..
///
}
}
}
现在我们要实现可以用于wap的page类了,WapPage.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.IO;
//Author:Sonven
//Blog:Sonven.cnblogs.comnamespaceRsion.Web
{
publicclassWapPage:Page
{
publicWapPage():base(){}
///<summary>///绑定事件
///</summary>protectedoverridevoidBindEvents()
{
Page.Load+=delegate(objects,EventArgse)
{
HttpContext.Current.Response.Write("<?xmlversion=\"1.0\"?>\r"+"<!DOCTYPEwmlPUBLIC\"-//WAPFORUM//DTDWML1.1//EN\"\"http://www.wapforum.org/DTD/wml_1.1.xml\">\r");};
Page.LoadComplete+=delegate(objects,EventArgse)
{
HttpContext.Current.Response.ContentType="text/vnd.Web.wml";
};
//处理错误时候转向错误页面[仅在发布后]#ifDEBUG#else
Page.Error+=delegate(objects,EventArgse)
{
Session["errormsg"]=HttpContext.Current.Error.Message+"<br/>"+"地址:"+HttpContext.Current.Request.RawUrl.ToString();
HttpContext.Current.Response.Redirect("~/error.aspx");
};
#endif
Page.PreRender+=delegate(objects,EventArgse)
{
};
}
}
}
这样就差不多只要继承WapPage就可以实现wap网页开发了
接下来我们创建模板,并给模板加上缓存提高性能
Application.cs用于提供缓存时间
(rsion.com,锐讯,巴中广州佛山成都网站建设,newmin,new.min,new.min@msn.com,newmin.net,巴中网站建设tel:18608275575锐讯)
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Web;
usingRsion.Web;
namespaceRsion.Web
{
publicabstractclassApplication:System.Web.HttpApplication
{
publicstaticTemplateTemplate;
///<summary>///模板缓存时间
///</summary>publicstaticintTemplateCacheTime=10;
///<summary>///重启Web进程
///</summary>publicstaticvoidRestartWebProcess()
{
HttpRuntime.UnloadAppDomain();
}
}
}
接下来我们创建一个单独的Template项目先
在里面创建Template.cs,ParamRules
ParamRules实现如下:
(rsion.com,锐讯,巴中广州佛山成都网站建设,newmin,new.min,new.min@msn.com,newmin.net,巴中网站建设tel:18608275575锐讯)
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Collections;
namespaceRsion.Web
{
///<summary>///模板参数规则类
///</summary>publicclassParamRules:IEnumerable
{
privateDictionary<string,object>rules;
publicParamRules()
{
if(rules==null)rules=newDictionary<string,object>();
}
publicobjectthis[stringparamKey]
{
get
{
if(rules.ContainsKey(paramKey))returnrules[paramKey];
return"";
}
set
{
if(rules.ContainsKey(paramKey))rules[paramKey]=value;
elserules.Add(paramKey,value);
}
}
///<summary>///添加新的规则
///</summary>///<paramname="paramKey"></param>///<paramname="paramValue"></param>publicvoidAdd(stringparamKey,objectparamValue)
{
if(rules.ContainsKey(paramKey))
thrownewArgumentException("对不起规则已经存在!Key:"+paramKey+",Value:"+rules[paramKey].ToString(),"paramKey");
rules.Add(paramKey,paramValue);
}
publicvoidRemove(stringparamKey,objectparamValue)
{
if(rules.ContainsKey(paramKey))
rules.Remove(paramKey);
}
#regionIEnumerable成员publicIEnumeratorGetEnumerator()
{
foreach(KeyValuePair<string,object>kinrules)
{
yieldreturnk;
}
}
#endregion
}
}
Template.cs实现如下:
(rsion.com,锐讯,巴中广州佛山成都网站建设,newmin,new.min,new.min@msn.com,newmin.net,巴中网站建设tel:18608275575锐讯)
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceRsion.Web
{
///<summary>///模板
///</summary>publicclassTemplate
{
publicstaticTemplate_template;
privatestaticParamRulesrules;
privateTemplate(){}
publicstaticTemplateCreateInstance()
{
if(_template==null)_template=newTemplate();
return_template;
}
publicParamRulesRules
{
get
{
if(rules==null)rules=newParamRules();
returnrules;
}
}
}
}
这样我们先在global.asax中填加一些模板数据,这样才可以解析模板,解析模板的功能实现在PageAdapter中,这样可以
在本页面直接调用Html.RenderPartial("template")调用
gobal.asax
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.SessionState;
usingRsion.Web;
usingc=Rsion.Web.Config;
namespaceRsion.Wap
{
publicclassGlobal:System.Web.HttpApplication
{
protectedvoidApplication_Start(objectsender,EventArgse)
{
InitTemplate();//初始化模板数据,只针对那些不经常变化的数据}
#regionevents
protectedvoidSession_Start(objectsender,EventArgse)
{
}
protectedvoidApplication_BeginRequest(objectsender,EventArgse)
{
}
protectedvoidApplication_AuthenticateRequest(objectsender,EventArgse)
{
}
protectedvoidApplication_Error(objectsender,EventArgse)
{
}
protectedvoidSession_End(objectsender,EventArgse)
{
}
protectedvoidApplication_End(objectsender,EventArgse)
{
}
#endregionprivatevoidInitTemplate()
{
global::Rsion.Web.Templatet=Template.CreateInstance();
//添加模板数据规则,只用于不常更新的数据如key=webname则{$webname}替换成valuet.Rules.Add("webname",c.Web.Current.WebName);
t.Rules.Add("weburi",c.Web.Current.WebUri);
t.Rules.Add("sysname","sonven'swapdevelopframework!");
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
两文件如下
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingRsion.Web.Config;
usingRsion.Web;
namespaceRsion.Wap
{
publicpartialclassDefault:WapPage
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!Page.IsPostBack)
{
TempData.Add("webname",Gobal.Web.WebName);
TempData.Add("webUri",Gobal.Web.WebUri);
}
}
}
}
default.aspx
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="Rsion.Wap.Default"%><wml><head></head><cardtitle="<%=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
<ahref="http://www.cnyolee.com">有理网</a>|
<ahref="http://sonven.cnblogs.com">博客园</a>|
<ahref="http://www.rsion.com">联系我</a><br/>
${webname}${weburi}
同理创建其他的模板
我们在Page类里面实现了友好的自定义错误页,我们创建显示这个页面的error.aspx
error.aspx
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Error.aspx.cs"Inherits="Rsion.Wap.Error"%><wml><cardtitle="对不起出错了!">
手机锐讯网Web.rsion.com<br/>
错误信息:<br/><%=TempData["errormsg"]%></card></wml>
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingRsion.Web;
namespaceRsion.Wap
{
publicpartialclassError:WapPage
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!Page.IsPostBack)
{
this.TempData["errormsg"]=Session["errormsg"]??"系统执行出错!";
}
}
}
}
Ok了接下来就该验收结果了,达开/moni在里面输入你的地址就可以看到wap已经可以正常在浏览器中显示了
关于模拟器的实现请看我的另篇文章
点此下载该项目代码