ASP. Summary of NET method to implement pseudo static web pages

  • 2021-01-14 05:47:33
  • OfStack

This paper summarizes the example of ASP.NET pseudo static web page method, to share with you for your reference. The specific methods are as follows:

Method 1: Rewrite URL with Httphandler (pseudo-URL and pseudo-static)

We sometimes meet to this address: "http: / / www. XXXX. com/show - 12-34 html", you might think that the site server root directory "/" is called "show - 12-34 html" file, there is no actual it might actually, and may you see content is "/ aspx show. aspx & # 63; type= 12 & id=34 ". Why do you want to do this? There are several reasons for this. Firstly, it is to improve the friendliness of URL, remember "show-12-34.html" is better than "/aspx/show.aspx? type=12 & id=34 "Is that easy to remember? The second is convenient search engine included, many search engines are more optimistic about static HTML page, such as: Baidu; The second reason is for security, because this hides the parameters "type", "id". Isn't that interesting?

URL rewrite this is used, the following I say 1 in ASP NET2. I know the most simple implementation under 0 method: by implementing an interface "IHttpHandler" to take over HTTP request, Follow me!

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


// class URLRewriter Program list:  
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 The class  
 /// Author:yoyo 
/// </summary> 
public class UrlRewriter : IHttpHandler // To achieve" IHttpHandler "Interface  
{ 
 public UrlRewriter() 
{ 
// 
// TODO:  Add the constructor logic here  
 // 
} 
public void ProcessRequest(HttpContext Context) 
{ 
try 
{ 
// Get original URL Mask out parameters  
string Url = Context.Request.RawUrl; 
// Creating regular expressions  
  System.Text.RegularExpressions.Regex Reg = new System.Text.RegularExpressions.Regex 
  (@"/show-(\d+)-(\d+)\..+",System.Text.RegularExpressions.RegexOptions.IgnoreCase); 
// Use regular expressions to match  
System.Text.RegularExpressions.Match m = Reg.Match(Url,Url.LastIndexOf("/"));// From the last 1 A" / "Start matching  
if (m.Success)// The match is successful  
{ 
String RealPath = @"~/aspx/show.aspx?type=" + m.Groups[1] + "&id=" + m.Groups[2]; 
//Context.Response.Write(RealPath); 
//Context.RewritePath(RealPath);//(RewritePath  In the not  Cookie  Session state. ) 
Context.Server.Execute(RealPath); 
} 
else  
{ 
Context.Response.Redirect(Context.Request.Url.ToString()); 
} 
} 
catch 
{ 
Context.Response.Redirect(Context.Request.Url.ToString()); 
} 
} 
/// <summary> 
///  To achieve" IHttpHandler The required member of the interface  
/// </summary> 
/// <value></value> 
/// Author:yoyo 
public bool IsReusable 
{ 
get { return false; } 
}
}

2. Add the following Settings to web.config file

in < system.web > Add the following code under the node:

< httpHandlers >
< add verb="*" path="*/show-?*-?*.aspx" type="UrlRewriter" / >
< /httpHandlers >

Explanation 1:

verb refers to one or more of the allowable actions "GET", "POST", "PUT", and the asterisk "*" indicates all allowable;

path refers to the matching path and supports simple wildcards;

type refers to the bound class name and includes the namespace (if any);

By the way, first you need to create an WEB form "show. aspx" under the directory "aspx", because this file is the page that actually accepts the request and displays the relevant content.

OK! , compile, open a website input address http: / / localhost: 80 / show - 12-34 aspx visit 1, check to see whether the display of "/ aspx show. aspx & # 63; type=12 & id=34 "? !

I am set up files matching ASPX above, because IIS. HTML extension. The default is not to ASP NET took over, if you want to take over HTML request,
Please map the extension of IIS.HTML to "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll",
Then change aspx from aspx to html:

< httpHandlers >
< add verb="*" path="*/show-?*-?*.html" type="UrlRewriter" / >
< /httpHandlers >

Now open a website input address http: / / localhost: 80 / show - 12-34 html access under 1 ~!

Method 2: address rewrite, use pseudo static, page postback processing

When making website pages, sometimes in order to user experience and search engine collection, many sites often use pseudo static address rewriting, or URL address rewriting method, to such as: MyEbook.aspx? ID=1,MyEbook.aspx? ID=2MyEbook.aspx? ID = 3, with a number of dynamic pages refs disguised as a static page, for example, will become My1 above disguise. html, My2. html, My3. html etc!

The starting point is very good, not only conducive to the user experience, but also conducive to the collection, is really one to give two!

Benefits already speak, are using this way to talk about the existing problems, the problem is, when you use a pseudo static technology page button, need to submit content, this time, between 1 denier and the server is back to the start, then, in the address bar shows the web address is no longer a fake My1. html, My2. html, My3. html etc, but into MyEbook. aspx & # 63; ID=1,MyEbook.aspx? ID=2MyEbook.aspx? ID=3 and so on, so 1, will let the web users do not understand the production of uneasy, they will think that they are on a fraud website, that their submitted data are another site to steal, in short, give them the feeling is that such a site has the nature of deception; Most likely, they will never dare to go to your website again!! ......

The following I personal experience to solve the above problems as the following explanation, hope to help the majority of netizens to solve the problem!

To solve the above problem, there are many ways to use Javascript to prohibit postback, there will be a page

Change and so on a variety of methods, anyway, can change the place, have been used, and, this article is more, but it is difficult to achieve, on my temper, prefer direct way, as little as possible to change the place, so as not to make mistakes in the future trouble;

Step 1: Open up your website and create a new class with a file name like this: MyActionlessform.cs

Step 2: The code for the 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; //  In disguise 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); 
    } 
  } 
} 

After that, compile the class file as MyActionlessform. dll and refer to it in the project on the website.

Step 3: open a postback page operation, will be one of: System. Web. UI. Page changed to: MyURL. MyOLPage, that's all, 1 LaoYongYi, later no longer fear the postback exposed to real address scare the net friend to pull!

So far, the great work can become 1 half more than 1 point, then the problem again, what problem, you run 1 your website, is not the following error ah!

CS0433: The 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\ NET\Framework\v2.0.50727\Temporary ASP.NET: WINDOWS\ Microsoft 3266 aab1 Files \ schoolit \ \ aca2fc74 \ assembly \ dl3 d107fc6 \ \ 4 00 b753fe_ea19c801 \ MyActionlessform DLL"

General is such a mistake, for such a mistake, there are a lot of solution, in some places, remove the temporary use of the folder, delete a namespace and so on, I've tried, but try the best may not be the fruit, later I will consider it myself, such mistakes is two files exist at the same time, but can't reuse, the solution that is not easy, stop using 1, using only one line

The above, we are not set up a MyActionlessform. cs yet, it exists App_Code inside this folder, the other one is MyActionlessform dll, two file content completely, 1 sample ready, we don't need MyActionlessform. cs, we only need MyActionlessform. dll, then, will try to get rid of the former not have (I suggest to this file to exclude projects, don't advocate to delete the file directly, because after may use to), I would be, MyActionlessform.cs file is directly discharged in the project. After testing, the great work has been completed !!!!

Method 3: Use Mircosoft URLRewriter.dll to realize pseudo-static page

Yesterday, re-posted 1 article ISAPI filter is used to realize URL pseudo static, according to the content in the article I do, but not do it, so far, this method doesn't use Microsoft URLRewriter. dll this method is good, at least I personally think, spent a night, finally worked out how to use the dll file to asp. net page for static, actually very simple.
dll = URLRewriter; dll = Mircosoft
Can get Mircosoft URLRewriter. dll http: / / www microsoft. com china/msdn library/webservices/asp net/URLRewriting mspx & # 63; mfr=true
Here, select [download the source code of this article], after downloading, import the project, I here did not make any modification to the project, retain the original rewrite way, and then directly in VS2005 inside the generation.dll file can be.
2. Use the dll file:

Add a reference. Done.

3. The design of the page is not described here, I will put a download package, interested friends to download and have a look, the code is messy.

4. web.config configuration
This is the key, and the key to the success of statics.


<?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 of 1:


<RewriterConfig> 
  <Rules> 
  <RewriterRule> 
   <LookFor> The schema to look for </LookFor> 
   <SendTo> The string to be used to replace the pattern </SendTo> 
  </RewriterRule> 
  <RewriterRule> 
   <LookFor> The schema to look for </LookFor> 
   <SendTo> The string to be used to replace the pattern </SendTo> 
  </RewriterRule> 
  </Rules> 
</RewriterConfig> 

httpHandlers setting is mainly to cooperate with IIS will request redefinition processing, here is also more critical, if there is no reasonable httpHandlers, then, the access will surely fail.

About regular expressions, you can go to baidu search :" commonly used regular expressions ", there will be a lot.

5. Configure IIS parsing.html file

Right click on my computer > Management - > Expand 'Services and Applications' > internet Information Service -- > Find your shared directory -- > Right-click on Properties > Click 'Configure' > The mapping below -- > Find the executable path of.aspx Copy path -- > Paste path -- > The extension is ".html "-- > Then remove the check box to check whether the file exists. If the "OK" button fails, you can use the keyboard event to edit the path.

The sample code of this article is downloaded from this site.

I believe that this article described to everyone's asp.net program design has a definite reference value.


Related articles: