URL rewriting is to rewrite the URL address (sweat^_^).
Details: http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx
Advantages: shorten url, etc.
Usage: 1. Download ms’s URLRewrite.dll and put it in your bin
2. Set the following settings in web.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/d(d+).aspx</LookFor>
<SendTo>~/default.aspx?id=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
<system.web>
<httpHandlers>
<add verb="*" path="*.aspx"
type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
Then write in cs:
private void Page_Load(object sender, System.EventArgs e)
{
//Put user code here to initialize the page
Response.Write(Request.QueryString["id"]+"<BR>");
Response.Write("haha");
}
Just enter
localhost/overred/d123.aspx (note: the beginning must be d, followed by a number)
In fact, this d123.aspx is virtual and does not actually exist. As long as it fits the format.
It will jump to http://localhost/overred/default.aspx
and it can capture some parameters in default such as id, which is the number after your d (the last must be a number), so that you can display the id as 123 article.
If a postback is generated in the rewritten URL, it will be passed to d123.aspx, so that the user will see the actual address when clicking the button. MSDN says: But from the user's perspective, if the user clicks the button They may be disturbed by sudden URL changes.
It can be seen that ms regards customers as his God! (Really? #¥%…—*)
Tocontinue quoting ms:
This happens because when the Web Form is rendered, it sets its action property directly to the value of the file path in the Request object. Of course, when the Web form is rendered, the URL has been rewritten from /Products/Beverages.aspx to ListProductsByCategory.aspx?CategoryID=1, indicating that the Request object reports that the user wants to access ListProductsByCategory.aspx?CategoryID=1. Simply making the server side form not render the action properties solves this problem. (By default, the browser will post back if the form does not contain action properties.)
Unfortunately, Web Forms does not allow you to specify action properties explicitly, nor does it allow you to set certain properties to disable the rendering of action properties. . Therefore, we must extend the System.Web.HtmlControls.HtmlForm class ourselves, override the RenderAttribute() method and explicitly state that it does not render action attributes.
Thanks to the inheritance feature, we can get all the functionality of the HtmlForm class and get the desired behavior by just adding a few lines of code. The following shows the complete code of the custom class:
namespace ActionlessForm {
public class Form : System.Web.UI.HtmlControls.HtmlForm
{
protected override void RenderAttributes(HtmlTextWriter writer)
{
writer.WriteAttribute("name", this.Name);
base.Attributes.Remove("name");
writer.WriteAttribute("method", this.Method);
base.Attributes.Remove("method");
this.Attributes.Render(writer);
base.Attributes.Remove("action");
if (base.ID != null)
writer.WriteAttribute("id", base.ClientID);
}
}
}
The code for the overridden RenderAttributes() method only contains the exact code for the RenderAttributes() method of the HtmlForm class, without setting the action attributes. (I used Lutz Roeder's Reflector to view the source code of the HtmlForm class.)
After you create this class and compile it, to use it in an ASP.NET web application, you should first add it to the web application's References file. Clamped. Then, to use it in place of the HtmlForm class, just add the following to the top of your ASP.NET web page:
<%@ Register TagPrefix="skm" Namespace="ActionlessForm"
Assembly="ActionlessForm" %>
Then, replace <form runat="server"> (if any) with:
<skm:Form id="Form1" method="post" runat="server">
and on the right < Replace the /form> tag with:
</skm:Form>
The above is to inherit a form. In fact, there is a simpler way, which is to inherit the page, so you don't need to change anything in the aspx page.
Code:
using System;
using System.IO;
using System.Web;
using System.Web.UI;
namespace URl
{
/**//// <summary>
/// Page base class www.downcodes.com
/// </summary>
public class OLPage : Page
{
public OLPage()
{
}
/**//// <summary>
/// Override the default HtmlTextWriter method and modify the value attribute in the form tag so that its value is the rewritten URL instead of the real URL.
/// </summary>
/// <param name="writer"></param>
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; // Fake URL
internal FormFixerHtml32TextWriter(TextWriter writer):base(writer)
{
_url = HttpContext.Current.Request.RawUrl;
}
public override void WriteAttribute(string name, string value, bool encode)
{
// If the currently output attribute is the action attribute of the form tag, replace its value with the rewritten fake URL
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);
}
}
}
You encapsulate it into a dll, and you can pull it later by just adding a reference!
ok, it is so easy!