日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#关于伪静态页面的两种实现方法

發布時間:2023/12/18 C# 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#关于伪静态页面的两种实现方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一種是在頁面global.asax中,相關代碼如下:

View Code 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);
????????}

第二種方法是在HttpModule.cs中,代碼如下:

View Code 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;
????????}


????}

轉載于:https://www.cnblogs.com/chendaoyin/archive/2013/01/27/2878629.html

總結

以上是生活随笔為你收集整理的C#关于伪静态页面的两种实现方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。