用asp.net开发移动wap网站集成在线wap模拟器

摘要:
newDictionary();ifdataArray[key]=value;elsedataArray.Add;}}//////添加一个键值数据/////////publicvoidAdd{dataArray=dataArray??
下面的代码将演示利用ASP.NET2.0开发WAP模拟器和支持移动设备浏览的网页
用asp.net开发移动wap网站集成在线wap模拟器第1张
注:通过目录下的/moni可以模拟浏览我们制作好的wap网站
我们先实现一个Page类,添加一些于aspx页的交互,因为wap可能不支持viewState吧
Page.cs 注意与System.Web.UI.Page分开
用asp.net开发移动wap网站集成在线wap模拟器第2张用asp.net开发移动wap网站集成在线wap模拟器第3张Code
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()
{
}
}
}
用asp.net开发移动wap网站集成在线wap模拟器第4张用asp.net开发移动wap网站集成在线wap模拟器第5张Code
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();
}
}
}
创建TempData用于与.aspx页数据交换
用asp.net开发移动wap网站集成在线wap模拟器第6张用asp.net开发移动wap网站集成在线wap模拟器第7张Code
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锐讯)

用asp.net开发移动wap网站集成在线wap模拟器第8张用asp.net开发移动wap网站集成在线wap模拟器第9张Code
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用asp.net开发移动wap网站集成在线wap模拟器第10张..
///
}
}
}

现在我们要实现可以用于wap的page类了,WapPage.cs

用asp.net开发移动wap网站集成在线wap模拟器第11张用asp.net开发移动wap网站集成在线wap模拟器第12张Code
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锐讯)

用asp.net开发移动wap网站集成在线wap模拟器第13张用asp.net开发移动wap网站集成在线wap模拟器第14张Code
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锐讯)

用asp.net开发移动wap网站集成在线wap模拟器第15张用asp.net开发移动wap网站集成在线wap模拟器第16张Code
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锐讯)

用asp.net开发移动wap网站集成在线wap模拟器第17张用asp.net开发移动wap网站集成在线wap模拟器第18张Code
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

用asp.net开发移动wap网站集成在线wap模拟器第19张用asp.net开发移动wap网站集成在线wap模拟器第20张Code
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
两文件如下

用asp.net开发移动wap网站集成在线wap模拟器第21张用asp.net开发移动wap网站集成在线wap模拟器第22张Code
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

用asp.net开发移动wap网站集成在线wap模拟器第23张用asp.net开发移动wap网站集成在线wap模拟器第24张Code
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="Rsion.Wap.Default"%><wml><head></head><cardtitle="<%=TempData["webname"]%>"><%Html.RenderPartial("top");%><%Html.RenderPartial("index");%>
数据绑定用
&lt;%=TempData[id]%&gt;<br/><br/>调用显示模板&lt;%Html.RenderPartial(&quot;<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

<br/><ahref="/">首页</a>|
<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

用asp.net开发移动wap网站集成在线wap模拟器第25张用asp.net开发移动wap网站集成在线wap模拟器第26张Code
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Error.aspx.cs"Inherits="Rsion.Wap.Error"%><wml><cardtitle="对不起出错了!">
手机锐讯网Web.rsion.com
<br/>
错误信息:
<br/><%=TempData["errormsg"]%></card></wml>
用asp.net开发移动wap网站集成在线wap模拟器第27张用asp.net开发移动wap网站集成在线wap模拟器第28张Code
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已经可以正常在浏览器中显示了
关于模拟器的实现请看我的另篇文章
点此下载该项目代码

免责声明:文章转载自《用asp.net开发移动wap网站集成在线wap模拟器》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇discuz 论坛配置 QQ/163 网易邮箱XAML学习笔记——Layout(四)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

DEP(数据执行保护)介绍

数据执行保护是一组软件和硬件技术,可以对内存执行额外检查,以帮助防止恶意代码在系统上运行。硬件实现DEP来检测从这些位置运行的代码,并在发现执行时抛出异常。此功能也称为非执行和执行保护。为了与DEP合作,AMD和微软共同设计并开发了AMD的新芯片功能“增强病毒防护”。[1] DEP的安全机制,即“数据执行保护”,是一种Windows安全机制,主要用于防止病毒...

产品人必备干货:产品开发流程(完整版)

以后的产品开发流程也可以参考此文档的环节进行开发。产品经理根据设计人员提供的要求需要提供以下内容:功能类:功能流程图、产品的功能点、主要功能卖点。每一个icon的设计要跟整体页面的风格保持一致,这样才能保证所有页面设计完成以后风格是保持统一的。...

mini.DataGrid使用说明

√√√ ajaxOptionsObjectajax配置对象。√√√ idFieldString是行数据的唯一字段。设置为“client”之后,客户端将排序√√√√ totalCountNumber记录总数√√√ defaultColumnWidthNumber默认列宽100√√√√ showColumnsBoolean显示标头true√√√√ showPag...

CAS单点登录------未认证授权服务

问题背景:之前我使用的127.0.0.1进行CAS直接url进行过滤!后来我用nginx进行反向代理出现问题:如下图第一眼,就在内心想,草这什么鬼!调试了五分钟发现还是不行!  网上各种教程!  半小时过去了!我吧配置文件自己看看!    出现这样问题:原因是CAS 的服务认真之前先有个REgx的正则判断,目录如上看这个正则时候,我瞬间明白了,我下面这个问题...

安装pygame

在python3中安装pygame库。一段时间后,您可以看到安装成功,并且可以导入pygame...

ORACLE无法删除当前连接用户

今天在做Oracle数据库是遇到ORACLE无法删除当前连接用户,经查找可用如下方法解决。SQL˃dropuseracascade;//删除用户以及用户表空间下所有对象用户已丢弃。...