Summary of methods for implementing pseudo-static Web pages using ASP. NET, asp.net
The examples in this article summarize the methods for implementing pseudo-static Web pages in ASP. NET and share them with you for your reference. The specific method is as follows:
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 SolutionThe class code 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 rewrite class // Author: yoyo // </summary> public class UrlRewriter: IHttpHandler // implement the "IHttpHandler" interface {public UrlRewr Iter () {// TODO: add the constructor logic here //} public void ProcessRequest (HttpContext Context) {try {// obtain the original URL to shield the 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); // use a regular expression to match the System. text. regularExpressions. match m = Reg. match (Url, Url. lastI NdexOf ("/"); // match if (m. Success) from the last "/" // match {String RealPath = @"~ /Aspx/show. aspx? Type = "+ m. groups [1] + "& id =" + m. groups [2]; // Context. response. write (RealPath); // Context. rewritePath (RealPath); // (RewritePath is used in the cookieless session state .) Context. server. execute (RealPath);} else {Context. response. redirect (Context. request. url. toString () ;}} catch {Context. response. redirect (Context. request. url. toString ());}} /// <summary> /// member required to implement the "IHttpHandler" interface /// </summary> /// <value> </value> // Author: yoyo public bool IsReusable {get {return false ;}}}
2. Add the following settings 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, 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) ;}} in Ternal 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 sending back operation, and change System. Web. UI. Page to MyURL. MyOLPage.That's all. Once and for all, I will not worry about sending back and exposing the real address to scare the netizens!
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.
Ii. Use the dll file:
Add reference.
3. Page DesignI will release a download package. If you are interested in downloading the package, you can see that the code is messy.
Iv. web. config Configuration
This is critical, and it is also the key to static-based success.
<?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>
Here is a brief introduction:
<RewriterConfig> <Rules> <RewriterRule> <LookFor> the mode to be searched </LookFor> <SendTo> string to replace the mode </SendTo> </RewriterRule> <RewriterRule> <LookFor> the mode to be searched </LookFor> <SendTo> string 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.
Click here to download the instance code in this article.
I believe this article has some reference value for your asp.net program design.
Implement pseudo-static aspnet website pages
1. Find a file named UrlRewriter. dll on the Internet and put it in the bin folder of the item directory.
2. Configure web. config
(1) Add <section name = "CustomConfiguration" type = "URLRewriter. Config. UrlsSection, URLRewriter"/>
(2) add <add type = "URLRewriter. RewriterModule, URLRewriter" name = "RewriterModule"/> to the httpModules tab.
(3) add in the configuration tag
<CustomConfiguration>
<Urls>
<Add virtualUrl = "~ /News (. [0-9] *) \. html "destinationUrl = "~ /Article. aspx? Id = $1 "/>
<Add virtualUrl = "~ /Newsddd (. +) "destinationUrl = "~ /News. aspx? Id = $1 "/>
<Add virtualUrl = "~ /List (. [0-9] *) "destinationUrl = "~ /List. aspx? Id = $1 "/>
<Add virtualUrl = "~ /Admin/Login "destinationUrl = "~ /Admin/Login. aspx "/>
<Add virtualUrl = "~ /Search "destinationUrl = "~ /Search. aspx "/>
</Urls>
</CustomConfiguration>
These are Rewrite Rules
How does aspnet implement pseudo-static
Hello! I'm glad to answer your questions!
For more information, see the attachment.
For more information, see www.69dns.com/help/hlp_dtl.asp? Nid = 10000114
I hope it will be helpful to you and hope to adopt it. Thank you !!!