C# 伪静态页面的二个实现代码
本文导语: 方法1,在页面global.asax中,参考代码如下: 代码示例: void Application_BeginRequest(object sender, EventArgs e) { HttpContext context = ((HttpApplication)sender).Context; string oldurl = context.Request.Path.ToLower(); if...
方法1,在页面global.asax中,参考代码如下:
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
{
var context = ((HttpApplication)sender).Context;
var request = context.Request;
var url = request.Url;
if (!VerifyUrl(/blog_article/url/index.html))
{
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();
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(/blog_article/Uri uri/index.html)
{
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;
}
}