Discuz! NT httpmodule redirection Method
Using system;
Using system. diagnostics;
Using system. Threading;
Using system. Web;
Using system. xml;
Using system. Text. regularexpressions;
Using system. IO;
Namespace Pureland. urlrewrite
{
/// <Summary>
/// Httpmodule class
/// </Summary>
Public class httpmodule: system. Web. ihttpmodule
{
/// <Summary>
/// Implement the init METHOD OF THE INTERFACE
/// </Summary>
/// <Param name = "context"> </param>
Public void Init (httpapplication context)
{
Context. beginrequest + = new eventhandler (reurl_beginrequest );
}
Public void application_onerror (Object sender, eventargs E)
{
}
/// <Summary>
/// Implement the interface's dispose method
/// </Summary>
Public void dispose ()
{
}
/// <Summary>
/// Rewrite the URL
/// </Summary>
/// <Param name = "sender"> event source </param>
/// <Param name = "E"> events that contain event data </param>
Private void reurl_beginrequest (Object sender, eventargs E)
{
Httpcontext context = (httpapplication) sender). context;
String requestpath = context. Request. Path. tolower ();
Context. rewritepath ("test. aspx ");
}
}
}
Add in Web. config
<Httpmodules>
<Add type = "Pureland. urlrewrite. httpmodule, Pureland. urlrewrite" name = "httpmodule"/>
</Httpmodules>
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,
We can see on the Internet that many friends use httphandle + server. Transfer to perform urlrewrite in Asp.net. In fact, this method is incorrect. First, httphandle cannot implement urlrewrite; second, server. transfer is a standard redirection, not urlrewrite.
In fact, you do not need to implement httphandle or httpmodule on your own to implement urlrewrite. Use several linesCodeIt can be easily implemented.
I will introduce that on a VM, the VM is different from its own server. You do not have the permission to modify IIS or install IIS plug-ins such as IIS rewrite. However, we can still easily complete the required functions.
The procedure is as follows: open global. asax. CS and locate protected void application_beginrequest (Object sender, eventargs E ). From the method name, I can guess what it is. Enter the following code:
Protected void application_beginrequest (Object sender, eventargs e) {string oldurl = httpcontext. current. request. rawurl; string pattern = @ "^ (. +) default/(\ D + )\. aspx (\?. *) * ___ Fckpd ___ 0 quot; string replace = "$ 1default. aspx? Id = $2 "; if (RegEx. ismatch (oldurl, pattern, regexoptions. ignorecase | regexoptions. compiled) {string newurl = RegEx. replace (oldurl, pattern, replace, regexoptions. compiled | regexoptions. ignorecase); this. context. rewritepath (newurl );}} |
With the above code, I access a code similar :... /default/123. aspx URL, of course, this URL does not exist on my computer, it will be directed :... /default. aspx? Id = 123.
Of course, with a powerful regular expression, you can rewrite the URL as needed. All of this is done silently on the server side and won't be noticed on the client side. Because it is on the virtual host, we can only redirect the. aspx file. If it is your own server, you only need to register the Suffix in IIS to implement any suffix processing. For example, you can register a type such as *. myweb, so that when someone accesses default/456. myweb, You can redirect it to default. aspx? Id = 456. In a word, as long as you can think of. net, it can help you implement it, and it does not require much code.