ASP. NET 2.0 implements the pseudo-static Web Page Method (reprinted ---- continued)

Source: Internet
Author: User

Method 1: Use httphandler to rewrite the URL (pseudo URL and pseudo static)

We sometimes see an address like this: "Why? Type = 12 & id = 34 ", why? There are multiple reasons: First, enhanced urlfriendliness. Remember show-12-34.html "total ratio"/aspx/show. aspx? Type = 12 & id = 34 "is it easy to remember? The second is to facilitate search engine indexing. Many search engines prefer static html pages, such as Baidu. The second is for security reasons because the parameters "type" and "ID" are hidden ". Is it interesting?

In fact, this is implemented using URL rewriting. Next I will talk about it in ASP. what I know in net2.0 is the simplest implementation method: The Implementation interface "ihttphandler" is used to take over HTTP requests, follow me!

1. Add a class to the resource management solution. The code of the class is as follows:

// List of urlrewriter programs:
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
/// <Summary>
/// Urlrewriter URL rewriting class
/// Author: Yoyo
// Blog: http://yangmingsheng.cn
/// </Summary>
Public class urlrewriter: ihttphandler // implements the "ihttphandler" interface.
{
Public urlrewriter ()
{
//
// Todo: add the constructor logic here
//
}
Public void processrequest (httpcontext context)
{
Try
{
// Obtain the original URL blocking Parameter
String url = context. Request. rawurl;
// Create a regular expression
System. Text. regularexpressions. RegEx Reg = new system. Text. regularexpressions. RegEx

(@ "/Show-(\ D +) \... +", system. Text. regularexpressions. regexoptions. ignorecase );
// Match with a regular expression
System. Text. regularexpressions. Match m =

Reg. Match (URL, URL. lastindexof ("/"); // match from the last "/"
If (M. Success) // match successful
{
String realpath =

@"~ /Aspx/show. aspx? Type = "+ M. Groups [1] +" & id = "+ M. Groups [2];
// Context. response. Write (realpath );
// Context. rewritepath (realpath); // (rewritepath is used in cookieless session state .)
Context. server. Execute (realpath );
}
Else

{
Context. response. Redirect (context. Request. url. tostring ());
}
}
Catch
{
Context. response. Redirect (context. Request. url. tostring ());
}
}
/// <Summary>
/// Members required to implement the "ihttphandler" Interface
/// </Summary>
/// <Value> </value>
/// Author: Yoyo
// Blog: http://yangmingsheng.cn
Public bool isreusable
{
Get {return false ;}
}
}

2. Add the setting items in the web. config file.

Add the following code under the <system. Web> node:

<Httphandlers>
<Add verb = "*" Path = "*/show -? *-? *. Aspx "type =" urlrewriter "/>
</Httphandlers>

Explanations:

Verb indicates one or more of the permitted actions "get", "Post", and "put". The asterisk "*" indicates that all actions are allowed;

A path is a matching path that supports Simple wildcards;

Type indicates the bound class name and namespace (if any );

By the way, you must first create a web form "show. aspx" under the directory "aspx", because this file is a page that actually accepts the request and displays the relevant content.

OK !, Compile, open the website input address http: // localhost: 80/show-12-34.aspx access, check to see if it is displayed "/aspx/show. aspx? What about type = 12 & id = 34 ?!

The above settings match the aspx file, because the. html extension in IIS is not taken over by ASP. NET by default. If you want to take over the HTML request,
Map the IIS extension. html to "C: \ WINDOWS \ Microsoft. NET \ framework \ v2.0.50727 \ aspnet_isapi.dll ",
Then, change the above aspx to HTML:

<Httphandlers>
<Add verb = "*" Path = "*/show -? *-? *. Html "type =" urlrewriter "/>
</Httphandlers>

Open the website now and enter the address http: // localhost: 80/show-12-34.html to visit ~!

Method 2:Address rewriting, pseudo-static processing, and page sending back

 

When creating a website page, for the user experience and search engine indexing, many websites often use pseudo-static address rewriting or URL address rewriting to rewrite such as: myebook. aspx? Id = 1, myebook. aspx? Id = 2myebook. aspx? Id = 3. A dynamic page with parameters is pseudo-styled as a static page. For example, the above pseudo-elements are my1.html, my2.html, my3.html, and so on!

The starting point is very good, not only for the user experience, but also for the collection, it is really a two-pronged!

The advantage has been mentioned. Now let's talk about the problem with using this method. The problem is that when you use a button on a page using pseudo-static technology and need to submit content, once there is a send-back action between the server and the server, the web page address displayed in the address bar is no longer a fake pulling of my1.html, my2.html, my3.html, etc., but is changed to myebook. aspx? Id = 1, myebook. aspx? Id = 2myebook. aspx? Id = 3 and so on. As a result, netizens who do not know much about website creation will feel uneasy and will think that they are on a scam website, I think that all the data I submitted has been stolen by another website. In short, I feel that such a website is fraudulent. It is very likely that, they will not dare to go to your website any more !!! .........

The following is my personal experience in solving the above problems, and I hope to help the majority of users solve the problem!

There are many ways to solve the above problem: use JavaScript to disable sending back, and use

There are many ways to change it. Some people have used it. In addition, there are many such articles, but it is difficult to implement them. I just have a bad temper and prefer the direct method, change the location as few as possible to avoid future errors;

Step 1: Open your website and create a new class. The file name is like this: myactionlessform. CS

Step 2: the code for this class is as follows:

Using system;
Using system. IO;
Using system. Web;
Using system. Web. UI;
Namespace myurl
{
Public class myolpage: Page
{
Public myolpage ()
{}
Protected override void render (htmltextwriter writer)
{
If (writer is system. Web. UI. html32textwriter)
{
Writer = new formfixerhtml32textwriter (writer. innerwriter );
}
Else
{
Writer = new formfixerhtmltextwriter (writer. innerwriter );
}
Base. Render (writer );
}
}

Internal class formfixerhtml32textwriter: system. Web. UI. html32textwriter
{
Private string _ URL; // disguised URL
Internal formfixerhtml32textwriter (textwriter writer)
: Base (writer)
{
_ Url = httpcontext. Current. Request. rawurl;
}
Public override void writeattribute (string name, string value, bool encode)
{
If (_ URL! = NULL & string. Compare (name, "action", true) = 0)
{
Value = _ URL;
}
Base. writeattribute (name, value, encode );
}
}
Internal class formfixerhtmltextwriter: system. Web. UI. htmltextwriter
{
Private string _ URL;
Internal formfixerhtmltextwriter (textwriter writer)
: Base (writer)
{
_ Url = httpcontext. Current. Request. rawurl;
}
Public override void writeattribute (string name, string value, bool encode)
{
If (_ URL! = NULL & string. Compare (name, "action", true) = 0)
{
Value = _ URL;
}
Base. writeattribute (name, value, encode );
}
}
}
Then, compile the class file into the myactionlessform. dll file and reference it in the project of the website,

Step 3: Open the page with the send-back operation. web. UI. page changed to myurl. myolpage, that's all. Once and for all, I will not worry about sending back and exposing the real address!

At this point, more than half of the major achievements can be made, and the next question comes again. What's the problem? Do you see the following error when you run your website!

Cs0433: type "myurl. myolpage also exists in "C: \ WINDOWS \ Microsoft. net \ framework \ v2.0.50727 \ temporary ASP. net files \ schoolit \ 3266aab1 \ aca2fc74 \ App_Code.2-69spm5.dll "and" c: \ windows \ Microsoft. net \ framework \ v2.0.50727 \ temporary ASP. net files \ schoolit \ 3266aab1 \ aca2fc74 \ Assembly \ dl3 \ 4d1_fc6 \ 00b753fe_ea19c801 \ myactionlessform. DLL"

There are many solutions to this problem. In some cases, I have tried to delete temporary folders, delete a namespace, and so on, but I couldn't try it. Later I thought about it myself. The error is that two files exist at the same time, but they cannot be used repeatedly. The solution is not simple, so I will stop using one, just use one.

We have not created a myactionlessform. CS, it exists in the app_code folder, and the other is myactionlessform. DLL. The content of the two files is exactly the same. CS, we only need myactionlessform. DLL, then, try to get the former (I suggest excluding this file project, do not advocate directly Delete this file, because it may be used in the future), my approach is, export myactionlessform directly in the project. the CS file has been tested. The major achievements are now over !!!!

 

Method 3: Use Mircosoft urlrewriter. DLL to implement pseudo-static page
Yesterday, I posted an article about using ISAPI filter to implement URL pseudo-static. I did it according to the content in the article, but it was not made. Currently, Microsoft urlrewriter is not used for this method. the DLL method is good. At least I personally think it took me a night to finally find out how to use this DLL file to implement static on the Asp.net page, which is actually very simple.
1. Obtain Mircosoft urlrewriter. dll:
Get Mircosoft urlrewriter. dll available to http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx? MFR = true
Here, select [download the source code of this article]. After the download is complete, import the project. I have not made any changes to this project, and keep the original rewrite method, then it is generated directly in vs2005. DLL file.
2. Use the DLL file:
Add reference.
3. The page design is not described here. I will put a download package. If you are interested, please download it and check that the code is messy.
4. Web. config Configuration
This is critical, and it is also the key to static-based success.
Instance download: http://files.cnblogs.com/zhangyi85/StaticWeb.rar

<? XML version = "1.0"?>
<Configuration>
<Configsections>
<Section name = "rewriterconfig" type = "urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"/>
</Configsections>

<Rewriterconfig>
<Rules>
<Rewriterrule>
<Lookfor> ~ /Web/New/type/(. [0-9] *) \. html </lookfor>
<Sendto> ~ /Web/New. aspx? Id = $1 </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /Web/index.html </lookfor>
<Sendto> ~ /Web/index. aspx </sendto>
</Rewriterrule>
</Rules>
</Rewriterconfig>
<System. Web>
<Httphandlers>
<Add verb = "*" Path = "*. aspx" type = "urlrewriter. rewriterfactoryhandler, urlrewriter"/>
<Add verb = "*" Path = "*. html" type = "urlrewriter. rewriterfactoryhandler, urlrewriter"/>
</Httphandlers>
<Compilation DEBUG = "true"/> </system. Web>
</Configuration>

Here is a brief introduction:

<Rewriterconfig>
<Rules>
<Rewriterrule>
<Lookfor> the mode to be searched </lookfor>
<Sendto> string used to replace the mode </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> the mode to be searched </lookfor>
<Sendto> string used to replace the mode </sendto>
</Rewriterrule>
</Rules>
</Rewriterconfig>

Httphandlers is mainly used in combination with IIS to redefine and process requests. This is also critical. If there is no reasonable httphandlers, the access will certainly fail.

You can search for regular expressions in Baidu: "common regular expressions", there will be many.

5. Configure the iis .html File
Right-click my computer --> Manage --> Expand 'services and Applications' --> Internet Information Service --> Find the directory you shared --> right-click Properties --> click 'configuration' --> under ing --> Find. aspx executable file path copy path --> paste path --> the extension is ". html "--> then remove the check object check box. If the" OK "button becomes invalid, you can use the keyboard event editing path to solve the problem.

This article from: http://www.cnblogs.com/xiachufeng/archive/2010/02/04/1663869.html

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.