Implementation of pseudo-static page in C # two ways to introduce _ practical skills

Source: Internet
Author: User
The first is in the page global.asax, the relevant code is as follows
Copy Code code 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 approach is in HttpModule.cs, where the code is as follows
Copy Code code 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;
}
}

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.