. Net pseudo-static page application

Source: Internet
Author: User

Of course, the program can directly generate static pages.
However, many such pages and folders need to be generated.
In addition, generating large amounts of data is a waste of time.
In this case, url rewriting is required. Of course there are many methods.
The following describes Microsoft's URLRewriter method.

1. Download URLRewriter. dll and copy it to the bin directory of your website.
2. Configure your web. config file
Add
<ConfigSections>
<Section name = "RewriterConfig" type = "URLRewriter. Config. RewriterConfigSerializerSectionHandler, URLRewriter"/>
</ConfigSections>

<RewriterConfig>
<Rules>
<! -- The three tests are written by myself. Of course, you can write many tests. -->
<! -- This is to set http: // localhost: 2001/default. aspx? Id = 1 generate a page like http: // localhost: 2001/d1.aspx -->
<RewriterRule>
<LookFor> ~ /D (\ d {0, 5}) \. aspx </LookFor>
<SendTo> ~ /Default. aspx? ID = $1 </SendTo>
</RewriterRule>
<! -- This indicates http: // localhost: 2001/article. aspx? Id = 1 generate a page like http: // localhost: 2001/1/a.html -->
<RewriterRule>
<LookFor> ~ /(\ D {0, 5})/a \. html </LookFor>
<SendTo> ~ /Article. aspx? Id = $1 </SendTo>
</RewriterRule>
<! -- This is to set http: // localhost: 2001/test. aspx? Id = 1 generate http: // localhost: 2001/1/This page -->
<RewriterRule>
<LookFor> ~ /(\ D {0, 5})/</LookFor>
<SendTo> ~ /Test. aspx? Id = $1 </SendTo>
</RewriterRule>

</Rules>

</RewriterConfig>

In the <system. web> Configuration section, add
<HttpHandlers>
<Add verb = "*" path = "*. aspx" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
<Add verb = "*" path = "*. html" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
<Add verb = "*" path = "*" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>

</HttpHandlers>

3. Okay. Basically, you can do it. But there is another important point. Never forget it.
There are three types of url generation
In the first case, you can complete the configuration in the second step.
You can configure the second and third types of URLs.
First, let's talk about the second situation: the reason is that they re-write the. html suffix.
In iis.html ing, the method is as follows:
Right-click site-properties-home directory-Configuration
Find the extension. aspx on the page, double-click it, and copy the executable file in the pop-up window.
C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ aspnet_isapi.dll
After copying, click Add to copy the content to the executable file in the pop-up window.
Of course, you can write this extension .html at will, but you need
<Add verb = "*" path = "*. html" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
Consistent.
Check whether the file exists.
Enter http: // localhost: 2001/1/a.html
Is it accessible? Refreshing

The following describes the third scenario: generate a directory
This is a little complicated.
In the pop-up window, click Insert.
Copy the file C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ aspnet_isapi.dll
Copy the file to the executable file box and remove it to check whether the file exists. OK.
In this way, enter http: // localhost: 2001/1/. Is it accessible? Hey
Readers may wonder: what is the difference between the current setting and the second setting?
Now we set * ing, that is, all the files are processed by the url rewriting handler.
In this case, the .html file can be taken down by a .aspx, And the .html/. aspx/url file can be generated in the same way.
Well, if you want to expand other extensions, you can do it as you like.

4. Now let's talk about * Notes for ing.
Because * ing means that all files must be handed over to the url appreciation program for processing, this will cause the images and css under the site to be processed.
Or will it be slow to process all downloaded files such as rar? The answer is no doubt, it will certainly increase the memory,
Images and uploaded images for a single website are enough to speed down the program exponentially.
Is there any way to deal with it?
Of course, we can perform the following operations to restore the speed:
Method 1: Convert the folder where the image is stored into a virtual directory, and then move it to the virtual directory *. * ing, so that his url rewriting program will not process image files. Of course, other files that do not require url rewriting must be processed similarly like image folders.
Virtual directory setting method: Right-click the directory point attribute on the iis Site and choose "directory-point creation ".
Click Configure and delete the *. * ing added under the wildcard application ing (execution sequence) in the pop-up window.
Method 2: Separate the folder that does not require *. * ing from the original site.
The domain name of the new site. For example, set the picture site http://images.ziuziu.cn, so that when the http://ziuziu.cn site to access the image must use http://images.ziuziu.cn/name.gif, this kind of avoiding all the pieces of processing.
The above two methods can increase the speed a lot, of course, the first method is much more convenient.

5. You can search for the final process of sending a response on the Internet.

For the ASPX page, when we view the code on the client, we will find that it automatically adds an action to the form, and the address is the original page address mentioned at the beginning. For An ASPX page, if its action is empty, it will be sent back to the current address. In this way, you can clear the action on the client to ensure that the address remains available after sending the response.

Add the following code to the page:
<Script type = "text/javascript"> try {document. forms [0]. action = ""} catch (ex) {}</script>
If there is a form page, add the above Code to <form> </form>. If it is a user control, directly add it to the page.
This method is the simplest and most useful method and has been tested.

 

Environment: Microsoft. NET Framework SDK v1.1
OS: Windows Server 2003 Chinese Version
ASP. Net generate static HTML pages
The FileSystemObject object used to generate static pages implemented in Asp!
System. IO is involved in. Net.
The following is the program code Note: This code is not original! Reference others' code
// Generate an HTML page
Public static bool WriteFile (string strText, string strContent, string strAuthor)
{
String path = HttpContext. Current. Server. MapPath ("/news /");
Encoding code = Encoding. GetEncoding ("gb2312 ");
// Read the Template File
String temp = HttpContext. Current. Server. MapPath ("/news/text.html ");
StreamReader sr = null;
StreamWriter sw = null;
String str = "";
Try
{
Sr = new StreamReader (temp, code );
Str = sr. ReadToEnd (); // read the file
}
Catch (Exception exp)
{
HttpContext. Current. Response. Write (exp. Message );
HttpContext. Current. Response. End ();
Sr. Close ();
}


String htmlfilename = DateTime. Now. ToString ("yyyyMMddHHmmss") + ". html ";
// Replace content
// At this time, the template file has been read into the variable named str
Str = str. Replace ("ShowArticle", strText); // ShowArticle on the template page
Str = str. Replace ("biaoti", strText );
Str = str. Replace ("content", strContent );
Str = str. Replace ("author", strAuthor );
// Write an object
Try
{
Sw = new StreamWriter (path + htmlfilename, false, code );
Sw. Write (str );
Sw. Flush ();
}
Catch (Exception ex)
{
HttpContext. Current. Response. Write (ex. Message );
HttpContext. Current. Response. End ();
}
Finally
{
Sw. Close ();
}
Return true;

This function is stored in the Conn. CS base class.
Reference in the Code for adding news: Project name: Hover
 
If (Hover. Conn. WriteFilethis. Title. Text. ToString), this. Content. Text. ToString), this. Author. Text. ToString )))
{
Response. Write ("added successfully ");
}
Else
{
Response. Write ("An error occurred while generating HTML! ");
}
-------------------------------------------------------------------------
Template page text.html code
-------------------------------------------------------------------------
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Title> ShowArticle </title>

<Body>

Biaoti
<Br>
Content <br>
Author
</Body>
</HTML>

Author "A Xian"

Related Article

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.