欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中实现伪静态页面两种方式介绍

程序员文章站 2024-03-05 14:22:33
第一种是在页面global.asax中,相关代码如下: 复制代码 代码如下: void application_beginrequest(object sender, ev...
第一种是在页面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;
}
}