我用Httphandler重写URL改地址栏文件扩展名,但是运行结果文件扩展名还是没有变,但是可以手动改变,请问怎么样才能使他运行处来时就改变扩展名

解决方案 »

  1.   

    using System;
    using System.Data;
    using System.Configuration;
    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;/// <summary>
    /// MyHandler 的摘要说明
    /// </summary>
    public class MyHandler : IHttpHandler
    {
        public MyHandler()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }    #region IHttpHandler 成员    public bool IsReusable
        {
            get { return false; }
        }    public void ProcessRequest(HttpContext context)
        {
            context.Response.Write("DuoTian");
            string path = context.Request.RawUrl;
            context.Server.Transfer(path.Replace(".html", ".aspx"));
        }    #endregion
    }<httpHandlers>
    <add verb="*" path="*.html" type="MyHandler"/>
    </httpHandlers>
      

  2.   

    context.Server.Transfer(path.Replace(".html", ".aspx")); 
    这句有问题, 应该是: Server.Execute(path.Replace(".aspx", ".html")); 
      

  3.   

    地址栏的文件扩展名还是没有改变,需要配置IIS吗?
      

  4.   

      public class UrlRewriter : IHttpHandler 
                    {
                     public UrlRewriter()
                    {
                 
                    }
                    public void ProcessRequest(HttpContext Context)
                    {
                    try
                    {
                    string Url = Context.Request.RawUrl;
                         System.Text.RegularExpressions.Regex Reg = new System.Text.RegularExpressions.Regex(@"/show-(\d+)-(\d+)\..+",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    System.Text.RegularExpressions.Match m =
                   Reg.Match(Url,Url.LastIndexOf("/"));
                    if (m.Success)
                    {
                    String RealPath =  @"~/aspx/show.aspx?type=" + m.Groups[1] + "&id=" + m.Groups[2];
                    Context.Server.Execute(RealPath);
                    }
                    else 
                                   {
                    Context.Response.Redirect(Context.Request.Url.ToString());
                    }
                    }
                    catch
                    {
                    Context.Response.Redirect(Context.Request.Url.ToString());
                    }
                    }
        
                    public bool IsReusable
                    {
                    get { return false; }
                    }
                    }
                     
    在<system.web>节点下添加如下代码:
                <httpHandlers>
                    <add verb="*" path="*/show-?*-?*.aspx" type="UrlRewriter" />
                    </httpHandlers>
    参考