使用UrlRewritingNet重写url以.html的伪静态页面后,真实的.html无法访问的解决方法

来源:互联网 发布:家常菜谱大全软件 编辑:程序博客网 时间:2024/04/29 19:05

使用UrlRewritingNet重写url.html的伪静态页面后,真实的.html无法访问的解决方法

1、在web.config中添加以下项目。

<compilation>
<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
</compilation>

<httpHandlers>
<add verb="*" path="*.html" type="System.Web.UI.PageHandlerFactory"/>
</httpHandlers>

2、重写处理方法

Namespace web

{

    public class eastClass : IHttpHandler

    {

        public bool IsReusable

        {

            get { return true; }

        }

 

        public void ProcessRequest(HttpContext context)

        {

            Uri url = context.Request.Url;

            string filepath = context.Request.Url.AbsolutePath;

            filepath = context.Server.MapPath("//") + filepath.Replace("/", "//");

            if (File.Exists(filepath) == false)

            {

                if (url.ToString().IndexOf("要处理的url中的关键字") > -1)

                {

                    string uname = url.Segments[url.Segments.Length - 1];//取出带后缀文件名称

                    string id = uname.Substring(0, uname.IndexOf("."));//真实的文件名

                    if (id != "")

                        context.Response.Redirect("/xxx.aspx?id=" + id, true);//跳转

                }

                else

                {

                    filepath = context.Server.MapPath("//") + "404.htm";

                    string tempstr;

                    using (StreamReader sr = new StreamReader(filepath, Encoding.Default))

                    {

                        tempstr = sr.ReadToEnd();

                    }

                    context.Response.Write(tempstr);

                    context.Response.End();

                }

            }

            else

            {

                string tempstr;

                using (StreamReader sr = new StreamReader(filepath, Encoding.UTF8))

                {

                    tempstr = sr.ReadToEnd();

                }

                context.Response.Write(tempstr);

                context.Response.End();

 

            }

        }

    }

}

 

 

3、充知识

buildProviders 元素定义生成提供程序的集合,这些提供程序用于在编译期间编译特定文件类型的自定义资源文件并生成代码。

若要为 ASP.NET 应用程序中的某个文件类型定义自定义生成操作,必须从 BuildProvider 类派生类,在派生类中实现用于生成该文件类型的成员并为应用程序配置文件中相应的文件扩展名配置生成提供程序。

下面是buildProvidersadd的属性信息:

属性

属性

说明

appliesTo

必选的属性。

定义要用此提供程序生成的资源文件的类型。

appliesTo 属性可以为下列可能值之一。

说明

All

指定生成提供程序在发现资源的任何位置编译资源。

Code

指定生成提供程序只编译 App_Code 目录中的资源。

Resources

指定生成提供程序只编译 App_GlobalResources 目录中的资源。

Web

指定生成提供程序只编译 Web-content 目录中的资源。

默认值为 Web

extension

必选的 String 属性。

定义自定义资源文件扩展名。此属性是集合键。

默认值为空字符串 ("")

type

必选的 String 属性。

定义用于编译 extension 属性指定的文件的类型和程序集。

默认值为空字符串 ("")

 

 

 

 

 

<httpHandlers> 设置由应用程序子目录继承。

<add> 指令按由上而下的顺序进行处理。如果两个或多个 <add> 元素指定相同的谓词/路径组合,则最后一个 <add> 会重写其他所有元素。

注意

Microsoft Internet 信息服务 (IIS) 有自己的 ISAPI 映射扩展模型。为使给定应用程序扩展与其处理程序之间的映射生效,该扩展必须在 IIS 中映射为 ASP.NET ISAPI。对于自定义扩展等非标准扩展,则必须相应地配置 IIS

下面是add属性信息:

属性

属性

说明

path

必选的属性。

指定路径属性可以包含单个 URL 路径或简单的通配符字符串(如 *.aspx)。

type

必选的属性。

指定逗号分隔的类/程序集组合。ASP.NET 首先在应用程序的专用 /bin 目录中搜索程序集 DLL,然后在系统程序集缓存中搜索程序集 DLL

verb

必选的属性。

指定谓词列表可以是逗号分隔的 HTTP 谓词列表(例如,"GET, PUT, POST"),也可以是开始脚本映射(如星号 [*] 通配符)。

validate

可选的属性。

如果为 false,则 ASP.NET 在实际匹配请求到达之前将不尝试加载该类。这有可能延迟错误,但减少了启动时间。

示例

下面的代码示例演示如何将对文件扩展名为 .New 的文件的所有 HTTP 请求映射到类 MyHandler.New,将对文件扩展名为 .MyNewFileExtension 的文件的 HTTP GET HEAD 请求映射到类 MyHandler.MNFEHandler.这两个类都在文件 MyHandler.dll 的程序集 MyHandler 中。

<configuration>

   <system.web>

      <httpHandlers>

         <add verb="*"

              path="*.New"

              type="MyHandler.New,MyHandler"/>

         <add verb="GET,HEAD"

              path="*.MyNewFileExtension"

              type="MyHandler.MNFEHandler,MyHandler.dll"/>

     </httpHandlers>

   <system.web>

</configuration>

原创粉丝点击