Introduction to implementing pseudo-static pages in C #

Source: Internet
Author: User
Tags servervariables

First, in the global. asax page, the related code is as follows::
Copy codeThe Code is as follows:
Void Application_BeginRequest (object sender, EventArgs e)
{
HttpContext context = (HttpApplication) sender). Context;
String oldurl = context. Request. Path. ToLower ();
If (oldurl. indexOf ("-")> 0 & oldurl. indexOf (". ") =-1) | (oldurl. indexOf ("-")> 0 & oldurl. indexOf ("aspx")> 0 ))
{
String [] url = oldurl. Substring (oldurl. LastIndexOf ("/") + 1). Replace (". aspx", ""). Split ('-');
String path = oldurl. Substring (0, oldurl. LastIndexOf ("/") + 1 );
// File
String file = url [0];
File = file. Replace ("about", "detail ");
File = file. Replace ("news", "list ");
File = file. Replace ("down", "detail ");
File = file. Replace ("case", "album ");
File = file. Replace ("contact", "detail ");
// Query
String query = "";
For (int I = 1; I <url. Length; I ++)
{
If (url [I]! = "")
{
Switch (I)
{
Case 1:
Query + = "id =" + url [I];
Break;
Case 2:
Query + = "& page =" + url [I];
Break;
Case 3:
Query + = "& key =" + url [I];
Break;
Case 4:
Query + = "& v1 =" + url [I];
Break;
Case 5:
Query + = "& v2 =" + url [I];
Break;
Case 6:
Query + = "& v3 =" + url [I];
Break;
Case 7:
Query + = "& v4 =" + url [I];
Break;
Case 8:
Query + = "& v5 =" + url [I];
Break;
Case 9:
Query + = "& v6 =" + url [I];
Break;
Case 10:
Query + = "& v7 =" + url [I];
Break;
}
}
}
// Newurl
String newurl = path + file + ". aspx? "+ Query;
If (context. Request. ServerVariables ["QUERY_STRING"]! = Null & context. Request. ServerVariables ["QUERY_STRING"]! = "")
Newurl + = "&" + context. Request. ServerVariables ["QUERY_STRING"];
// Response. Write (newurl );
Context. RewritePath (newurl );
}

The second method is in HttpModule. cs, the Code is as follows::
Copy codeThe Code is as follows:
Public class HttpModule: IHttpModule
{
Private const RegexOptions regexOptions = RegexOptions. IgnoreCase | RegexOptions. Compiled;
Private static readonly Regex regexFileName = new Regex (@".*? /([^./] *) \. Aspx (. *) ", regexOptions );
Private static readonly Regex regexRewritePath = new Regex (@ "^ .*? /(\ W *)(-? (\ W +)-([\ w, \ |, %] +) + \. aspx ", regexOptions );
Public void Dispose ()
{
}
Public void Init (HttpApplication httpApplication)
{
HttpApplication. BeginRequest + = ReUrl_BeginRequest;
}
Private static void ReUrl_BeginRequest (object sender, EventArgs e)
{
Globals. Catch (
() =>
{
Var context = (HttpApplication) sender). Context;
Var request = context. Request;
Var url = request. Url;
If (! VerifyUrl (url ))
{
String input = url. PathAndQuery. ToLower ();
// Loger. Debug ("PathAndQuery -->" + input );
// Loger. Debug ("AbsolutePath -->" + url. AbsolutePath );
// Loger. Debug ("AbsoluteUri -->" + url. AbsoluteUri );
// Loger. Debug ("DnsSafeHost -->" + url. DnsSafeHost );
// Loger. Debug ("LocalPath -->" + url. LocalPath );
// Loger. Debug ("AppDomain. CurrentDomain. BaseDirectory -->" + AppDomain. CurrentDomain. BaseDirectory );
// Loger. Debug ("Globals. GlobalsVirtualFilePath -->" + Globals. GlobalsVirtualFilePath );
If (input. StartsWith (Globals. GlobalsVirtualFilePath ))
Input = input. Remove (0, Globals. GlobalsVirtualFilePath. Length );
String viewmode = Globals. ViewMode;
Var themeName = request. QueryString ["theme"]? "";
If (string. IsNullOrEmpty (themeName ))
{
ThemeName = Globals. ThemeName;
}
If (input = "/")
Input = "/index. aspx ";
If (viewmode = "Rewrite ")
{
Loger. Debug ("now input -->" + input );
Match match = regexRewritePath. Match (input );
If (match. Success & match. Groups. Count = 5)
{
Var captures3 = match. Groups [3]. Captures;
Var captures4 = match. Groups [4]. Captures;
Var itemCount = match. Groups [3]. Captures. Count;
Var list = new List <string> ();
For (var I = 0; I <itemCount; I ++)
{
List. Add (string. Concat (captures3 [I]. Value, "=", captures4 [I]. Value ));
}
Context. RewritePath (Globals. AspxFileUrl (themeName, match. Groups [1]. Value + ". aspx? "+ String. Join (" & ", list. ToArray ())));
Return;
}
}
Var fileName = regexFileName. Match (request. Path. ToLower (). Groups [1]. ToString ();
If (string. IsNullOrEmpty (fileName ))
Return;
New ConvertTheme (context)
{
ThemeName = themeName,
ViewMode = viewmode
}. Display (fileName );
}
});
}
Private static bool VerifyUrl (Uri uri)
{
Var url = uri. AbsolutePath. ToLower ();
If (url. StartsWith (Globals. GlobalsVirtualFilePath ))
Url = url. Remove (0, Globals. GlobalsVirtualFilePath. Length );
Return uri. IsFile
| Url. IndexOf ("site ")! =-1
| Url. IndexOf ("sys ")! =-1
| Url. IndexOf ("html ")! =-1
| Url. IndexOf ("user ")! =-1
| Url. IndexOf ("bbs ")! =-1
| Url. IndexOf ("_ module. aspx ")! =-1
| Url. IndexOf ("webresource. axd ")! =-1
| Url. IndexOf ("scriptresource. axd ")! =-1;
}
}

Related Article

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.