主頁(yè) > 知識(shí)庫(kù) > C#中實(shí)現(xiàn)偽靜態(tài)頁(yè)面兩種方式介紹

C#中實(shí)現(xiàn)偽靜態(tài)頁(yè)面兩種方式介紹

熱門(mén)標(biāo)簽:蘋(píng)果手機(jī)凱立德地圖標(biāo)注 百度ai地圖標(biāo)注 合肥電銷(xiāo)外呼系統(tǒng)哪家公司做的好 同安公安400電話(huà)怎么申請(qǐng)流程 玉林市機(jī)器人外呼系統(tǒng)哪家好 申請(qǐng)400電話(huà)手續(xù) 預(yù)測(cè)式外呼系統(tǒng)使用說(shuō)明 電話(huà)機(jī)器人軟件銷(xiāo)售工作 南陽(yáng)外呼系統(tǒng)定制化
第一種是在頁(yè)面global.asax中,相關(guān)代碼如下
復(fù)制代碼 代碼如下:

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;iurl.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);
}

第二種方法是在HttpModule.cs中,代碼如下
復(fù)制代碼 代碼如下:

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 Liststring>();
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;
}
}
您可能感興趣的文章:
  • C#實(shí)現(xiàn)動(dòng)態(tài)生成靜態(tài)頁(yè)面的類(lèi)詳解
  • C#用委托BeginInvoke做異步線(xiàn)程
  • mongodb使用c#驅(qū)動(dòng)數(shù)據(jù)插入demo
  • C# Winform實(shí)現(xiàn)石頭剪刀布游戲
  • c#操作mongodb插入數(shù)據(jù)效率
  • C#引用類(lèi)型和值類(lèi)型的適用場(chǎng)合和區(qū)別
  • C#自動(dòng)類(lèi)型轉(zhuǎn)換與強(qiáng)制類(lèi)型轉(zhuǎn)換的講解
  • 解析在C#中接口和類(lèi)的異同
  • C#實(shí)現(xiàn)圖片切割、切圖、裁剪
  • C#網(wǎng)站生成靜態(tài)頁(yè)面的實(shí)例講解

標(biāo)簽:南昌 淄博 南京 臺(tái)州 嘉興 揚(yáng)州 海南 南京

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《C#中實(shí)現(xiàn)偽靜態(tài)頁(yè)面兩種方式介紹》,本文關(guān)鍵詞  中,實(shí)現(xiàn),偽,靜態(tài),頁(yè)面,兩種,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《C#中實(shí)現(xiàn)偽靜態(tài)頁(yè)面兩種方式介紹》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于C#中實(shí)現(xiàn)偽靜態(tài)頁(yè)面兩種方式介紹的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章