Use urlrewrite and Asp.net to dynamically generate HTM pages (note 2)

Source: Internet
Author: User

A few days ago I wrote two essays about generating static pages during URL rewriting.

Use urlrewrite and Asp.net to dynamically generate HTM pages
Use urlrewrite and Asp.net to dynamically generate HTM pages (additional description)
Today, I sorted out the original ideas. I used to need two classes (ModulerewriterAndCreatehtmfactoryhandler).ModulerewriterI drew a flowchart.

KeyModulerewriterCode
Using system;
Using system. Text. regularexpressions;
Using system. configuration;
Using urlrewriter. config;
Using system. Data;
Using system. Web;
Using system. Web. UI;

Namespace urlrewriter
{
/** // <Summary>
/// Provides a rewriting httpmodule.
/// </Summary>
Public class modulerewriter: basemodulerewriter
{
/** // <Summary>
/// This method is called during the module's beginrequest event.
/// </Summary>
/// <Param name = "requestedrawurl"> the rawurl being requested (required des path and querystring). </param>
/// <Param name = "app"> the httpapplication instance. </param>
Protected override void rewrite (string requestedpath, system. Web. httpapplication APP)
{
// Only valid for the file suffix ASPX page
If (requestedpath. indexof (". aspx ")! =-1)
{
Httpcontext = app. context;

// Get the configuration rules
Rewriterrulecollection rules = rewriterconfiguration. getconfig (). Rules;

// Iterate through each rule
For (INT I = 0; I <rules. Count; I ++)
{
// Get the pattern to look for, and resolve the URL (convert ~ Into the appropriate directory)
String lookfor = "^" + rewriterutils. resolveurl (httpcontext. Request. applicationpath, rules [I]. lookfor) + "$ ";

// Create a RegEx (note that ignorecase is set)
RegEx Re = new RegEx (lookfor, regexoptions. ignorecase );

// See if a match is found
If (Re. ismatch (requestedpath ))
{
// The ASPX page is redirected to the HTM page and the HTTP data transmission mode is "get"
If (rules [I]. type = webtype. Static & App. Context. Request. requesttype = "get ")
{
// Static Page path
String htmlweb = rewriterutils. resolveurl (httpcontext. Request. applicationpath, rules [I]. sendto );
// Static Page physical path
String htmpath = httpcontext. server. mappath (htmlweb );
// Last modification time of the Static Page
Datetime dt = system. Io. file. getlastwritetime (htmpath );
If (datetime. compare (datetime. now. addhours (-1), DT) <= 0) // compare the current time with the static page generation time. If the time is less than one hour, redirect to the static page.
{
Requestedpath = htmlweb;
}
Else // compare the current time with the time when static pages are generated. If the time is greater than one hour, set the response filter to generate static pages.
{
Httpcontext. response. Filter = new responsefilter (httpcontext. response. filter, htmpath );
}
}
Else
{
Requestedpath = Rules [I]. sendto;
}

// Rewrite the URL
Rewriterutils. rewriteurl (httpcontext, requestedpath );
Break; // exit the For Loop
}
}
}
}

}
}


The source code provided now is the latest, and the date is. The changes include:
1. cache the regular expression instance
2. Use the thread pool to improve efficiency
3. Modified other code to reduce the waiting time for accessing the page.
Download original code and demo

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.