C#中实现伪静态页面的方式
2019-09-17 10:02:36来源:爱站网 阅读 ()
你知道C#中如何实现伪静态页面吗?今天小编将为大家介绍C#中实现伪静态页面的方式,其实这个功能并不难实现,主要还是要靠我们个人学习,现在我们就一起去看看吧。
第一种是在页面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<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中,代码如下:
?
?
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;
}
}
上文就是C#中实现伪静态页面的方式,现在伪静态技术以及出现了,它带动了于搜索引擎友好C#中实现伪静态页面有两种方式。?
原文链接:https://js.aizhan.com/develop/aspnet/8854.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- asp.net源程序编译为dll文件并调用的实现过程 2020-03-29
- WPF实现带全选复选框的列表控件 2020-03-29
- PHP session实现购物车功能 2020-03-23
- WPF实现简单的跑马灯效果 2020-03-23
- ASP.NET使用AjaxPro实现前端跟后台交互详解 2020-03-19
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash