教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 asp.net伪静态(URL重写)代码一例

asp.net伪静态(URL重写)代码一例

发布时间:2016-03-31   编辑:jiaochengji.com
本文介绍下,用asp.net实现伪静态页面,即url重写的一个实例代码,有需要的朋友,可以参考下。

实现伪静态的代码如下。

首先,加载用到的类。
 

复制代码 代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using Norco.DAL;
/
/// <summary>
/// Summary description for UrlRewriter
/// </summary>
public class SupportWJ : IHttpHandler
{
public SupportWJ()
{
//
// TODO: Add constructor logic here
//
}
public void ProcessRequest(HttpContext Context)
{
try
{
//取得原始URL屏蔽掉参数
string Url = Context.Request.RawUrl;
//建立正则表达式
//string sql = "insert into test(TestName) values(@test)";
//SqlParameter tmp = new SqlParameter("@test", SqlDbType.NVarChar, 2500);
//tmp.Value = Url;

//DbAccess.SQLExecuteNoneQuery(sql, tmp);
System.Text.RegularExpressions.Regex Reg = new System.Text.RegularExpressions.Regex(@"/NORCO-Support-(\d+)\.shtml", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
//用正则表达式进行匹配
System.Text.RegularExpressions.Match m = Reg.Match(Url, Url.LastIndexOf("/NORCO-Support-"));//从最后一个“/”开始匹配

//String RealPath = @"/about.aspx?id=" + m.Groups[1];
if (m.Success)//匹配成功
{
String RealPath = @"/Servers.aspx?sid=" + m.Groups[1];

//string sql = "insert into test(TestName) values(@test)";
//SqlParameter tmp = new SqlParameter("@test", SqlDbType.NVarChar, 50);
//tmp.Value = RealPath;
 

//DbAccess.SQLExecuteNoneQuery(sql, tmp);
 

//Context.Response.Write(RealPath);
//Context.RewritePath(RealPath);//(RewritePath 用在无 Cookie 会话状态中。)
Context.Server.Execute(RealPath);
}
else
{
Context.Response.Write("404 ERROR!");
}
}
catch
{
Context.Response.Redirect(Context.Request.Url.ToString());
}
}
/**/
/// <summary>
/// 实现“IHttpHandler”接口所必须的成员
/// </summary>
/// <value></value>
/// Author:yoyo
/// www.jbxue.com
public bool IsReusable
{
get { return false; }
}

}

然后,在在web.config添加:
 

<add verb="GET" path="*/NORCO-Support-*.shtml" type="SupportWJ"/>

最后,配置增加对shtml后缀的类ASPX处理。

完成以上三步,即可实现asp.net中伪静态页面,有兴趣的朋友,自己动手测试下吧。

您可能感兴趣的文章:
伪静态几种做法
asp.net伪静态后真正的静态文件无法访问的解决方法
php 伪静态 url重写简单示例
asp.net伪静态(URL重写)代码一例
php 伪静态(url重写)的写法
aspnet_isapi.dll实现无后缀名的url重写(伪静态)
C# .NET自定义类实现伪静态页面的代码
IIS7 伪静态支持环境配置(修改web.config)
C# 伪静态 URL重写配置一例
asp.net 伪静态简单实例

[关闭]
~ ~