摘要
從頁面Url及頁面名稱上看,你會發現靜態頁面和偽靜態是一樣的。偽靜態的頁面后綴可能是html,htm,cshtml等,只是改變了url的表現形式,實際上還是動態的頁面。在SEO方面,偽靜態和靜態頁面的功能是相同,但偽靜態本質上還是動態頁面,不會像靜態頁面那樣占用服務器空間資源。
UrlRewrite
這里通過Url重寫的方式實現偽靜態。
首先通過Nuget安裝UrlRewrite包。
修改web.config,添加如下內容
<?xml version="1.0" encoding="utf-8"?> <!-- 有關如何配置 ASP.NET 應用程序的詳細信息,請訪問 http://go.microsoft.com/fwlink/?LinkId=301880 --> <configuration> <configSections> <section name="CustomConfiguration" type="URLRewriter.Config.UrlsSection, URLRewriter" /> </configSections> <appSettings> <add key="webpages:Version" value="3.0.0.0"/> <add key="webpages:Enabled" value="false"/> <add key="ClientValidationEnabled" value="true"/> <add key="UnobtrusiveJavaScriptEnabled" value="true"/> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5"/> <httpRuntime targetFramework="4.5"/> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false"/> <modules runAllManagedModulesForAllRequests="true"> <remove name="UrlRoutingModule"/> <add name="UrlRoutingModule" type="UrlRewrite.RewriteModule, UrlRewrite" preCondition="managedHandler"/> </modules> </system.webServer> <CustomConfiguration> <urls> <!--([\w]+)表示,1到n個字母或數字或下划線或漢字組成--> <add virtualUrl="~/Index.html" destinationUrl="~/Home/Index" /> <add virtualUrl="~/(\d+)/Detail.html" destinationUrl="~/Home/Detail/?guid=$1" /> </urls> </CustomConfiguration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"/> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="1.0.0.0-5.2.0.0" newVersion="5.2.0.0"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
添加的內容如下:
<configSections> <section name="CustomConfiguration" type="URLRewriter.Config.UrlsSection, URLRewriter" /> </configSections>
<system.webServer> <validation validateIntegratedModeConfiguration="false"/> <modules runAllManagedModulesForAllRequests="true"> <remove name="UrlRoutingModule"/> <add name="UrlRoutingModule" type="UrlRewrite.RewriteModule, UrlRewrite" preCondition="managedHandler"/> </modules> </system.webServer> <CustomConfiguration> <urls> <!--([\w]+)表示,1到n個字母或數字或下划線或漢字組成--> <add virtualUrl="~/Index.html" destinationUrl="~/Home/Index" /> <add virtualUrl="~/(\d+)/Detail.html" destinationUrl="~/Home/Detail/?guid=$1" /> </urls> </CustomConfiguration>
然后,在路由配置中,將html的路由配置上。
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Index.html", url: "{controller}/{action}.html", defaults: new { controller = "Home", action = "Index" } ); routes.MapRoute( name: "Index", url: "{controller}/{action}", defaults: new { controller = "Home", action = "Index" } ); } }
到這里已經結束了,我們可以通過Home/index或者home/index.html兩種方式訪問首頁。
瀏覽
總結
看到偽靜態頁面和動態頁面實際上是一樣的。但*.html的物理文件在服務器上是不存在的。