教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 asp.net 伪静态简单实例

asp.net 伪静态简单实例

发布时间:2016-04-05   编辑:jiaochengji.com
本文介绍下,asp.net实现的,一个简单的伪静态实例代码,有需要的朋友,参考下吧。

首先,新建一个类,如:类名为URLRerite ,继承IHttpHandlerFactory接口。
 

复制代码 代码示例:

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;

/// <summary>
/// URLRerite 的摘要说明
/// </summary>
public class URLRerite : IHttpHandlerFactory
{

    #region IHttpHandlerFactory 成员
    public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
    {
        string path = url;
        string extend = Path.GetExtension(path);
        string getFileName = Path.GetFileNameWithoutExtension(path);
        string sendpath = path.Replace(extend, ".aspx");
        string filepath = pathTranslated;
        string qurstring = "";
        if (context.Request.QueryString.Count > 0)
        {

            qurstring = context.Request.QueryString.ToString() ;
        }
        // 重写URL
        filepath = context.Server.MapPath(sendpath);
        context.RewritePath(sendpath, String.Empty, qurstring);
        return PageParser.GetCompiledPageInstance(sendpath, filepath, context);

    }

    public void ReleaseHandler(IHttpHandler handler)
    {
        //throw new Exception("The method or operation is not implemented.");
    }
    #endregion
}

然后,在web.config文件中的<system.web>下面添加:
 

复制代码 代码示例:
<httpHandlers>
   <add verb="*" path="*.shtml" type="URLRerite"/>
</httpHandlers>

如此,一个简单的asp.net伪静态功能就实现了,适合新手朋友参考,高手请飘过。

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

[关闭]
~ ~