您的位置:首页 > 编程语言 > ASP

asp.net url 重写(伪静态)

2009-11-20 09:46 381 查看
 url重写,顾名思义,就是把原来的url地址进行重定向,原来做过的网站有些地方用到了这样的技术,但是由于自我感觉有些地方还是不太清楚,所以到现在为止,我有些问题还是没有得到解决。实现url重写的方法有多种,这里介绍的是我在工作中用到的重写方法。url重写最大的好处最主要用处应该是对搜索引擎的友好,其次是网站的安全性,所以这个技术在我们的工作中也是很有必要取学习,掌握的。现在把url重写具体的配置写一下,方便大家的学习,不在之处,请多多指教。

 

首页,在url重写的时候,我们需要一个URLRewriter.dll文件,这个百度一下,然后下载到本地。需要在配置文件中配置的东西我已用红色字体标注。

 

下面是在web.config中的配置

 

<?xml version="1.0"?>
<!--
    注意: 除了手动编辑此文件以外,您还可以使用
    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
     “网站”->“Asp.Net 配置”选项。
    设置和注释的完整列表在
    machine.config.comments 中,该文件通常位于
    /Windows/Microsoft.Net/Framework/v2.x/Config 中
-->
<configuration>
 <!--url重写 -->
 <configSections>
  <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
 </configSections>
 <!--url重写 -->
 <appSettings>
  <add key="FCKeditor:BasePath" value="../fckeditor/"/>
  <add key="FCKeditor:UserFilesPath" value="~/upload/"/>
 </appSettings>
 <connectionStrings>
  <add name="oleconstring" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|db_info.mdb" providerName="System.Data.OleDb"/>
 </connectionStrings>

 <system.web>
  <!--URL重写,定义支持.html的文件-->
  <compilation debug="true">
   <buildProviders>
    <add extension=".html" type="System.Web.Compilation.PageBuildProvider"/>
   </buildProviders>
   <assemblies>
    <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
   </assemblies>
  </compilation>
  <!--URL重写-->
  <!--
            设置 compilation debug="true" 将调试符号插入
            已编译的页面中。但由于这会
            影响性能,因此只在开发过程中将此值
            设置为 true。
        -->
  <!--
            通过 <authentication> 节可以配置 ASP.NET 使用的
            安全身份验证模式,
            以标识传入的用户。
        -->
  <authentication mode="Windows"/>
  <!--
            如果在执行请求的过程中出现未处理的错误,
            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
            开发人员通过该节可以配置
            要显示的 html 错误页
            以代替错误堆栈跟踪。

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
  <customErrors mode="RemoteOnly" defaultRedirect="404.htm">
   <error statusCode="403" redirect="NoAccess.htm"/>
   <error statusCode="404" redirect="FileNotFound.htm"/>
  </customErrors>
  <!--URL重写-->
  <httpHandlers>
   <remove path="*.asmx" verb="*"/>
   <add path="*.asmx" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
   <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false"/>
   <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/>
  </httpHandlers>
  <!--URL重写-->
  <sessionState cookieless="false" timeout="20"/>
 </system.web>
 <RewriterConfig>
  <Rules>
   <!-- 首页-->
   <RewriterRule>
    <LookFor>~/index.html</LookFor>
    <SendTo>~/index.aspx</SendTo>
   </RewriterRule>
  </Rules>
  <Rules>
   <!-- 新闻详细页-->
   <RewriterRule>
    <LookFor>~/ShowNewsDetails_n(/d+).html</LookFor>
    <!--<LookFor>~/PageInfoShow_n(/d+)</LookFor>-->
    <SendTo>~/ShowNewsDetails.aspx?scdTitleId=$1</SendTo>
   </RewriterRule>
  </Rules>
  <Rules>
   <!-- 新闻详细页-->
   <RewriterRule>
    <LookFor>~/SinglePage_(/d+).html</LookFor>
    <!--<LookFor>~/PageInfoShow_n(/d+)</LookFor>-->
    <SendTo>~/SinglePage.aspx?titleId=$1</SendTo>
   </RewriterRule>
  </Rules>
  <Rules>
   <!-- 新闻详细页-->
   <RewriterRule>
    <LookFor>~/News(/d+).html</LookFor>
    <!--<LookFor>~/PageInfoShow_n(/d+)</LookFor>-->
    <SendTo>~/News.aspx</SendTo>
   </RewriterRule>
  </Rules>
  <Rules>
   <!-- 新闻详细页-->
   <RewriterRule>
    <LookFor>~/Picture_(/d+).html</LookFor>
    <!--<LookFor>~/PageInfoShow_n(/d+)</LookFor>-->
    <SendTo>~/Picture.aspx?titleId=$1</SendTo>
   </RewriterRule>
  </Rules>
  <Rules>
   <!-- 新闻详细页-->
   <RewriterRule>
    <LookFor>~/ShowPicDetails_(/d+).html</LookFor>
    <!--<LookFor>~/PageInfoShow_n(/d+)</LookFor>-->
    <SendTo>~/ShowPicDetails.aspx?picId=$1</SendTo>
   </RewriterRule>
  </Rules>
  <Rules>
   <!-- 新闻详细页-->
   <RewriterRule>
    <LookFor>~/Message.html</LookFor>
    <!--<LookFor>~/PageInfoShow_n(/d+)</LookFor>-->
    <SendTo>~/Message.aspx?titleId=$1</SendTo>
   </RewriterRule>
  </Rules>
  <Rules>
   <!-- 新闻详细页-->
   <RewriterRule>
    <LookFor>~/SearchResult.html</LookFor>
    <!--<LookFor>~/PageInfoShow_n(/d+)</LookFor>-->
    <SendTo>~/SearchResult.aspx?className=$1&ProName=$2</SendTo>
   </RewriterRule>
  </Rules>
 </RewriterConfig>
</configuration>

 

这样配置文件写好后,我们在访问其中的某一个页面的时候,就可以直接访问了,
比如我们访问index.aspx 就可以访问index.html了,这样的话它就会自动去找index.aspx这个页面了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息