C#僞靜態實現的方法

 

  在asp.net開發網站的時候,咱們常常會用到僞靜態,好處是能夠隱藏真實的路徑,提升網站的安全性,在官網等展現網站但願對搜索引擎友好,提升搜索排名;或者在涉及到模板開發都會用到僞靜態。下面講解下平時用到的僞靜態實現方法。html

1、方法一:web

  使用URLRewriter.dll下載地址:http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi正則表達式

  1在啓動文件中添加URLRewriter.dll文件引用,而後在web.config文件configuration節點下添加一下代碼安全

<configSections>
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
  </configSections>

  2在system.web節點下添加一下代碼asp.net

<compilation>
      <buildProviders>
        <add extension=".html" type="System.Web.Compilation.PageBuildProvider"/>
      </buildProviders>
    </compilation>


<httpHandlers> <add verb="*" path="*.html" type="System.Web.UI.PageHandlerFactory"/> </httpHandlers> <httpModules> <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/> </httpModules>

 

  3在configuration節點下添加一下重寫url代碼(正則表達式)ide

<RewriterConfig>
    <Rules>
        
      <RewriterRule>
        <LookFor><![CDATA[~/shop/(\w+)?]]></LookFor>
        <SendTo><![CDATA[~/vshop/wechat/order/index.aspx?uid=$1]]></SendTo>
      </RewriterRule>

      
      <RewriterRule>
        <LookFor><![CDATA[~/tupian/(\d+)/.html]]></LookFor>
        <SendTo><![CDATA[~/web/admin/other/$1.ashx]]></SendTo>
      </RewriterRule>
        
    </Rules>
  </RewriterConfig>

2、方法二
在.net3.5版本開始,提供了System.Web.Routing,程序能夠本身寫僞靜態方法函數

添加一個ReWriteUrl.cs文件,代碼以下:網站

public class ReWriteUrl : IRouteHandler { public string UrlRote { get; private set; } public ReWriteUrl (string sUrlRote) { UrlRote = sUrlRote; } public IHttpHandler GetHttpHandler(RequestContext requestContext) { return BuildManager.CreateInstanceFromVirtualPath(UrlRote, typeof(IHttpHandler)) as IHttpHandler; } }

 

在Global.asax.cs文件下的Application_Start函數裏ui

 

 protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add( new Route("xxxx.html", new ReWriteUrl("~/xxxx.ashx")));//地址重寫
        }

.net技術交流QQ羣:320128737搜索引擎

相關文章
相關標籤/搜索