下面的代码将演示利用ASP.NET2.0开发WAP模拟器和支持移动设备浏览的网页


注:通过目录下的/moni可以模拟浏览我们制作好的wap网站
我们先实现一个Page类,添加一些于aspx页的交互,因为wap可能不支持viewState吧
Page.cs 注意与System.Web.UI.Page分开

Code
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()
        {
        }
    }
}

Code
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();
        }
    }
}

创建TempData用于与.aspx页数据交换

Code
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锐讯)

Code
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..
           ///
       }
    }
}

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

Code
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锐讯)

Code
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锐讯)

Code
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锐讯)

Code
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

Code
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
 两文件如下

Code
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

Code
<%@ 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"); %>
    数据绑定用&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 />
<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

Code
<%@ 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>

Code
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已经可以正常在浏览器中显示了
关于模拟器的实现请看我的另篇文章
点此下载该项目代码

转载于:https://www.cnblogs.com/newmin/archive/2009/10/12/asp_net_wap_developframework.html

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

  1. 基于微软平台IIS/ASP.NET开发的大型网站有哪些?

    首先说明一下,本文绝不是要说Microsoft平台多么好,多么牛.只是要提醒一些LAMP/JAVA平台下的同志们,微软平台不至于像你们说的,和想象的那么不堪!只是你们自己不知道而已.同时,也希望广大M ...

  2. WAP网站流量分析 WAP流量统计 wap网站流量统计

    随着3G在中国的发展以及手机上网用户的增长,免费wap做为手机广告的载体,业内人士预测在未来的中国无线网络营销将大放异彩. WAP的兴盛标志着市场已经走向成熟,WAP站长迫切需要如何了解用户,把握用户 ...

  3. asp.net开发中自定义网站的目录

    用asp.net做过几个项目,一直觉得有点儿不爽的地方,建立新的项目[asp.net]默认只能创建在c:\inetpub\wwwroot\目录下面! 几天发现只要你预先创建了相应的虚拟目录比如weba ...

  4. 用asp.net开发的网站的运行环境

    我用asp.net开发了一个网站,但是存在以下问题: 1 . 如果要把该网站放到服务器上,服务器需要安装那些运行环境? 服务器的操作系统是2000server已经装有IIS,不知道还需要装些什么. 2 ...

  5. WAP开发问答(1)简单的说WAP代表什么?

    利用WAP实现的网络业务以其移动性.灵活性.个人化.信息实时性.信息简短实用而受到广大数字移动电话用户的普遍欢迎.据预测,到2002年,中国Internet用户将达6500万,其中移动上网的用户可达3 ...

  6. asp毕业设计——基于asp+access的商品销售网站设计与实现(毕业论文+程序源码)——商品销售网

    基于asp+access的商品销售网设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于asp+access的商品销售网设计与实现,文章末尾附有本毕业设计的论文和源码下载地址哦. 文章目录: ...

  7. 手机wap网站建置方案

    开发案例 0 引言 手机的功能从简单的语音通信和文字短信, 发展 到现在的彩信.多媒体.无线上网??用户能够通过 手机来实现各种交换和获取丰富的信息资源.WAP (Wireless Applicati ...

  8. 手机wap网站建设的方法和技巧

    随着互联网技术的不断进步,越来越多的运营商对于手机wap网站的建设有了更多的投入,手机wap网站的建设和开发要根据网站的特点和经营范围来进行设计和建设,这样才可以提升手机wap网站建设的效果.现在智能 ...

  9. 用ASP技术进行动态WAP网站开发实例

    第一节:WAP的潜能 这些日子,我们常听到WAP技术,一种手机上网的技术.从技术上讲,移动电话不可能和PC来竞争,移动电话的屏幕只能容下很少的字符,它的带宽也是很受限制,而且输入也很笨拙.那我为什么还 ...

最新文章

  1. echarts y轴添加点击事件_ECharts中的事件和行为
  2. RNA-seq中的生物学重复
  3. html css网站开发兵书,程序员藏经阁--HTML+CSS网站开发兵书(附光盘)
  4. Docker创建 tomcat/weblogic 集群
  5. neuroph轻量级神经网络框架
  6. 【OpenCV】8邻域种子填充法剔除短连通域的高效算法
  7. 常用的HTTP响应头
  8. 北师大计算机组成原理答案,计算机组成原理(白中英)本科生试题库整理附答案[共21页].doc...
  9. python自动化学习_Python自动化学习笔记(二)
  10. 工业互联网平台基本架构
  11. 爬虫教程( 6 ) --- 爬虫 进阶、扩展
  12. spss入门——简单的数据预处理到时间序列分析系列(五)
  13. [1] DevOps 自动化运维工具Chef----入门
  14. Linux下通过命令行登录北交校园网
  15. [技术干货] zabbix docker安装详细教程
  16. 认识一下阿里的AI殿堂-达摩院
  17. Android应用CPU实时监控工具-全机型适用
  18. Linux:库函数:libc: glibc
  19. [R语言学习笔记] - R语言及Rstudio配置合集
  20. 正则匹配问号_跟BBEdit学正则表达式,轻松地学习晦涩难解的语法

热门文章

  1. 华通-小天 IDC
  2. Win10系统,如何让我的电脑显示在桌面上?
  3. 中国证券登记结算有限责任公司2023年度信息技术专业人员招聘正式启动
  4. 中国台湾Getac制造商首次在平板电脑中嵌入LiFi技术
  5. java九九_java九九乘法表
  6. golang http client跳过安全证书校验
  7. 何钦铭版C语言第3章答案,何钦铭版c语言第3章答案(17页)-原创力文档
  8. 水平集详解与代码分析一
  9. 一元多项式加法c语言,C语言一元多项式加法.doc
  10. 【QT学习】实现鼠标滑动触发的轮播图