现在的位置: 首页 > 综合 > 正文

.NET4.0下web应用程序用UrlRewriter.dll重写无后缀路径

2012年12月31日 ⁄ 综合 ⁄ 共 1919字 ⁄ 字号 评论关闭

示例环境:VS2010

要求:

              重写前:http://localhost:13275/Default.aspx?username=wilson

              重写后:http://localhost:13275/wilson

第一步:下载相关DLL

                         
 下载链接   :
ActionlessForm.dll和UrlRewriter.dll)

第二步:VS2010创建测试网站应用程序,并添加以上DLL的引用

                         PS:   操作步骤省略,我想都会
                                              

第三步:在项目中添加asp.net文件(App_Browsers)

 这个我们比较少用:http://book.51cto.com/art/200906/132822.htm
                        
                         1.选择项目--->右键添加--->添加asp.net文件夹--->App_Browsers
                          2.在App_Browsers下创建Form.browser文件
                                                
            <browsers>
                   <browser refID="Default">
                        <controlAdapters>
                              <adapter controlType="System.Web.UI.HtmlControls.HtmlForm"
                                     adapterType="URLRewriter.Form.FormRewriterControlAdapter" />
                         </controlAdapters>
                   </browser>
            </browsers>

第四步:配置web.config

                       1 .在configuration节点下添加
                                               
     
             <configSections>
                     <section name="CustomConfiguration" type="URLRewriter.Config.UrlsSection, URLRewriter"/>
             </configSections>
  
             <CustomConfiguration>
                    <urls>
                         <!--([\w]+)表示,1到n个字母或数字或下划线或汉字组成-->
                         <add virtualUrl="~/([\w]+)*" destinationUrl="~/Default.aspx?username=$1"/>
                    </urls>
             </CustomConfiguration>

                                           2.在System.web节点下添加
             <httpModules>
                    <add type="URLRewriter.RewriterModule, URLRewriter" name="RewriterModule"/>
             </httpModules>

第五步:测式

                  1.在项目Default.aspx.cs文件Load事件中加入
                     
            if (!IsPostBack)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("当前所在位置:Default.aspx<br/>");
                if (!string.IsNullOrEmpty(Request.Params["username"]))
                {
                    sb.Append("所接收到的参数username:" + Request.Params["username"]);  
                }
                Response.Write(sb.ToString());
            }

                 测试一:找开----->http://localhost:13275/Default.aspx   
                                 结果------>当前所在位置:Default.aspx 
                      
               

                 测试二:找开----->http://localhost:13275/wilson
                                 结果------>当前所在位置:Default.aspx
                                                   所接收到的参数username:wilson
                      

第六步:在IIS7.5里配置  http://blog.csdn.net/porschev/article/details/6894698

                         

抱歉!评论已关闭.