软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 网络编程 > ASP.NET > Asp.net开发的wap网站集成在线wap模拟器

Asp.net开发的wap网站集成在线wap模拟器

  俺用的工具有点落后,vs2008 express版。没用过asp.net mobile 开发过wap网站,于是想尽各种办法用asp.net 给实现了:

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

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

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页数据交换 

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 (用于添加模板支持)
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>
/// 显示模板
/// </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>
/// 转换该页的标签内容
/// </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)
{
};
}
}
}

相关阅读
网友评论
栏目导航
推荐软件