Wap websites developed by Asp.net integrate online wap Simulators

Source: Internet
Author: User

The tools used by Alibaba Cloud are a little outdated. vs2008 express edition. I have never used asp.net mobile to develop wap websites, so I tried my best to use asp.net for implementation:

Note: The/moni in the directory can simulate browsing the created wap website. First, we implement a Page class and add some aspx Page interactions, because wap may not support viewState, page. cs and System. web. UI. separate pages.

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>
/// Temporary page data
/// </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>
/// Bind an event
/// </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;
/// <Summary>
/// Template Cache Time
/// </Summary>
Public static int TemplateCacheTime = 10;
/// <Summary>
/// Restart the Web process
/// </Summary>
Public static void RestartWebProcess ()
{
HttpRuntime. UnloadAppDomain ();
}
}
}
Create TempData for data exchange with. aspx pages

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 ("this data item is not added to enter this set! "," TKey ", null );
}
Set
{
DataArray = dataArray ?? New Dictionary <TKey, TValue> ();
If (dataArray. ContainsKey (key) dataArray [key] = value;
Else dataArray. Add (key, value );
}
}
/// <Summary>
/// Add a key value data
/// </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 );
}
}
}
Expand the Page class to create a PageAdapter. cs (used to add template Support)
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 secondary adapter class
/// </Summary>
Public class PageAdapter
{
Private Page;
Public PageAdapter (Page page)
{
This. page = page;
}

/// <Summary>
/// Display template
/// </Summary>
/// <Param name = "partialPath"> template file path: without the suffix [template suffix. tpl]. For example, the bottom. tpl file under Templates is displayed. </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 ();
}
// Conversion
TransformTemplateTags (ref templateContent );
// Write buffer
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>
/// Convert the template content
/// </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>
/// Convert the label content of the page
/// </Summary>
Public void TransformPageTags ()
{
///
/// TO: DO ..
///
}
}
}
Now we need to implement the page class that can be used for wap, 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>
/// Bind an event
/// </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 ";
};

// Turn to the error page when handling errors [only after release]

# If DEBUG
# Else
Page. Error + = delegate (object s, EventArgs e)
{
Session ["errormsg"] = HttpContext. Current. Error. Message + "<br/>" +
"Address:" + HttpContext. Current. Request. RawUrl. ToString ();
HttpContext. Current. Response. Redirect ("~ /Error. aspx ");
};
# Endif

Page. PreRender + = delegate (object s, EventArgs e)
{
};
}
}
}

  • Three pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • Next Page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.