日期:2014-05-18  浏览次数:20731 次

关于伪静态问题

.net生成的文件名是aspx.我想在URL上把aspx改成html
C# code

protected override void Render(HtmlTextWriter writer)
    {
        string name = Request.Url.AbsolutePath.Substring(1, Request.Url.AbsolutePath.Length - 1).Replace("aspx", "html");
        name = name.ToLower().Replace("aspx/", "");
        string newurl = "";
        if (name.IndexOf("/") > 0)
        {
            newurl = Server.MapPath("/") + name;
        }
        else
        {
            newurl = Server.MapPath("/") + name;
        }
        MemoryStream ms = new MemoryStream();
        StreamWriter sww = new StreamWriter(ms);
        StreamWriter swr = new StreamWriter(newurl);
        HtmlTextWriter htmlw = new HtmlTextWriter(swr);
        base.Render(htmlw);
        htmlw.Flush();
        htmlw.Close();
        string strLL = System.Text.Encoding.UTF8.GetString(ms.ToArray());
        Response.Write(strLL);
        string tourl = Request.Url.AbsoluteUri.ToLower().Replace(".aspx", "htm").Replace("aspx/", "");
        Response.Redirect(tourl, true);
    }


这是我网上找到的一种方法。报一个映射错误.请问高手这个方法可以做伪静态用么

------解决方案--------------------
搜尋一下,應該有很多資料可參考
這篇寫的不錯
http://www.cnblogs.com/xiachufeng/archive/2010/02/04/1663866.html
------解决方案--------------------
LZ的做法用到了好几个Strame,并且还用到了Response.Redirect(),实在不妥
一般都是在HttpModule中截获请求,利用虚URL映射得到真实URL

http://www.cnblogs.com/NicholasLee/archive/2010/11/10/1873750.html

另外IIS7中已经自带了这个扩展模块


------解决方案--------------------
探讨
<rewriter>
<rewrite url="~/test_([a-zA-Z]+).html$" to="~/Show.aspx?Code=$1" processing="stop" />
</rewriter>
比如这行代码.Show代表我的页面.我所有页面都配置么

------解决方案--------------------
XML code
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />

configSections节点后
<RewriterConfig>
    <Rules>
      <RewriterRule>
        <LookFor><![CDATA[~/(\d+)\.htm]]></LookFor>
        <SendTo><![CDATA[~/index.aspx?pageNum=$1]]></SendTo>
      </RewriterRule>
    </Rules>
</RewriterConfig>


system.web节点里
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>