问题描述:我说细点请大家先认真看问题。1,是用虚拟主机,所以仅能用<httpModules>的形式,如果是不是虚主机,这个问题可能好办.
2,重写后:原www.abc.com/news.aspx?id=5
          重写为:www.abc.com/5.aspx
3,目前正常:但如果页面的post的行为,则变为:www.abc.com/news.aspx?id=5,也就是说露出真相了。说明:查以前贴子有同样问题,但大家的回答都是太简单,或没看清楼主的问题。思归给答了两回,我没看明白,大家别笑。我希望这个贴能给大家一个完整答案。正确的给高分,和我一样不会的请收藏本贴并up一下,up一下也有分。谢谢!

解决方案 »

  1.   

    http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx上面不是说得很清楚了么?特别是“处理回发”这一节,都讲了如何处理URL,并在要回发的页面使用这个form类:
    namespace ActionlessForm {
      public class Form : System.Web.UI.HtmlControls.HtmlForm
      {
         protected override void RenderAttributes(HtmlTextWriter writer)
         {
            writer.WriteAttribute("name", this.Name);
            base.Attributes.Remove("name");        writer.WriteAttribute("method", this.Method);
            base.Attributes.Remove("method");        this.Attributes.Render(writer);        base.Attributes.Remove("action");        if (base.ID != null)
               writer.WriteAttribute("id", base.ClientID);
         }
      }
    }
    这样后就不会变URL了
      

  2.   

    参考:
    http://www.cnblogs.com/hd/archive/2005/06/21/178367.html
      

  3.   

    学习,我对C#还不太懂,VB.net的还好
      

  4.   

    context.RewritePath() 用这个吧,绝对不会露馅。
      

  5.   

    http://time-is-life.cnblogs.com/articles/292861.html
      

  6.   

    using System.IO;
    using System.Web;
    using System.Web.UI;namespace TEST
    {
    /// <summary>
    /// PageBase 的摘要说明。
    /// </summary>
    public class PageBase : Page
    {
    public PageBase()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    } //// <summary>
    ///  重写默认的HtmlTextWriter方法,修改form标记中的value属性,使其值为重写的URL而不是真实URL。
    /// </summary>
    /// <param name="writer"></param>
    protected override void Render(HtmlTextWriter writer)
    { if (writer is System.Web.UI.Html32TextWriter)
    {
    writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
    }
    else
    {
    writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
    } base.Render(writer);
    }
    }
    internal class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter
    {
    private string _url; // 假的URL internal FormFixerHtml32TextWriter(TextWriter writer):base(writer)
    {
    _url = HttpContext.Current.Request.RawUrl;
    } public override void WriteAttribute(string name, string value, bool encode)
    {
    // 如果当前输出的属性为form标记的action属性,则将其值替换为重写后的虚假URL
    if (_url != null && string.Compare(name, "action", true) == 0)
    {
    value = _url;
    }
    base.WriteAttribute(name, value, encode);
    }
    }    
    internal class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter
    {
    private string _url;
    internal FormFixerHtmlTextWriter(TextWriter writer):base(writer)
    {
    _url = HttpContext.Current.Request.RawUrl;
    } public override void WriteAttribute(string name, string value, bool encode)
    {
    if (_url != null && string.Compare(name, "action", true) == 0)
    {
    value = _url;
    } base.WriteAttribute(name, value, encode);
    }
    }
    }
    然后让你所有的后台代码都从PageBase继承就可以了
      

  7.   

    http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx
      

  8.   

    我下载了M$的URLRewrite的例子回来,运行时它的根目录下有一个default.aspx文件,输入1后提交却不能重写URL,出现是:http://localhost/RewriterTester/ListProductsByCategory.aspx?CategoryID=1,而且,M$上说可以直接输入http://localhost/RewriterTester/Products/Beverages.aspx来运行,但是第一次运行时输入就会出现404错误,只有先运行http://localhost/RewriterTester/Products/,然后再输入http://localhost/RewriterTester/Products/Beverages.aspx才可以,
    为什么呢?
      

  9.   

    其实不用看明白的,我也没看明白,但是加个类就可以了。namespace ActionlessForm
    {
    /// <summary>
    /// The Form class extends the HtmlForm HTML control by overriding its RenderAttributes()
    /// method and NOT emitting an action attribute.
    /// </summary>
    public class Form : System.Web.UI.HtmlControls.HtmlForm
    {
    /// <summary>
    /// The RenderAttributes method adds the attributes to the rendered &lt;form&gt; tag.
    /// We override this method so that the action attribute is not emitted.
    /// </summary>
    protected override void RenderAttributes(HtmlTextWriter writer)
    {
    // write the form's name
    writer.WriteAttribute("name", this.Name);
    base.Attributes.Remove("name"); // write the form's method
    writer.WriteAttribute("method", this.Method);
    base.Attributes.Remove("method"); // remove the action attribute
    base.Attributes.Remove("action"); // finally write all other attributes
    this.Attributes.Render(writer); if (base.ID != null)
    writer.WriteAttribute("id", base.ClientID);
    } }
    }然后 
    <%@ Register TagPrefix="zdil1" Namespace="ActionlessForm" Assembly="ZDILURLRewriter"%><zdil1:form id="Form1" method="post" runat="server">
    ...</form>就可以了。
      

  10.   

    如果要重写的网址上包含有服务器端Web Form并执行数据回送,当该Web Form回送数据时会暴露出真实的网址,也就是说,当用户访问/Products/Baverage.aspx时,浏览器上地址栏显示的也是/Products/Baverage.aspx,但是实际上是访问/ListProdutsByCategoryID.aspx?CategoryID=1的内容,如果ListProductsByCategoryID.aspx页面执行了数据回送的话,用户被数据回送定向给原始的/ListProductByCategoryID.aspx?CategoryID=1页面上,而不是/Products/Baverage.aspx页面。这虽然不是什么大问题,但是用户会觉察到点击一个按钮时网址发生了的变化,这也许会令人不安,因为如果出于网址安全的角度来说,直接把真实的网址暴露出来了。
    之所以发生这种现象的原因是当Web Form在呈现之时就明确地设置其action属性为当前Request对象中文件路径的值。当然,在Web Form呈现之时,从/Produts/Baverage.aspx到/ListProductsByCategoryID.aspx?CategoryID=1的网址重写就已经执行完毕了,这意味着Request对象所汇报的是当前用户所访问的地址是/ListProductsByCategoryID.aspx?CategoryID=1。这么看来,只需让该服务器端表单在呈现之时不呈现action属性即可解决问题了。(对浏览器来说,如果不设置action属性的话,那么在提交的时候将使用其默认值。)
    然而不幸的是该Web Form不会允许你指定action属性,也不会允许你通过设置一些属性来达到禁用呈现action属性的目的。得自行继承System.Web.HtmlControls.HtmlForm这个类,并重载该类的RenderAttribute()方法,明确指出该类不呈现acton属性。
    感谢继承这个强大的功能,使得我们很简单就获取了HtmlForm这个类下所有的功能定义,只需少量几行代码就达到所需目的,完整代码楼上的代码: 
    对RenderAttributes()方法重载的代码包含了原类HtmlForm的RenderAttributes()方法全部的代码内容,只是简单地去掉了设置action属性这一节。(我参考了Lutz Roeder的Reflecter一文中类HtmlForm的源代码)
    当创建并编译了这个类后,将其添加到引用目录即可在该ASP.NET Web应用程序中使用。为了将原有HtmlForm类替换,只需简单地在页面顶部添加下列代码:
    <%@ Register TagPrefix="zdil1" Namespace="ActionlessForm" Assembly="ZDILURLRewriter"%>
    然后将<Form runat=”server”>标签替换为<zdil1:form id="Form1" method="post" runat="server">
    并将结束标记</Form>替换为</zdil1:form>上面的 </form> 改为 </zdil1:form>。
      

  11.   

    简单的说就是 修改了一下 action的属性 ,name、method、action 把这三个改成了地址栏里的信息。
      

  12.   

    我觉得用context.RewritePath()这个不错