内容简介:[Asp.net MVC]页面伪静态实现
摘要
从页面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的物理文件在服务器上是不存在的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 前端框架快速开发静态页面
- golang实现页面静态化操作
- 利用Django徒手写个静态页面生成工具
- 做 1 个静态页面要价 12 万,有错吗?
- Spring Boot--Thymeleaf模板引擎/静态页面
- jsp 使用request为页面添加静态数据的实例
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Operating System Algorithms
Nathan Adams、Elisha Chirchir / CreateSpace Independent Publishing Platform / 2017-4-21 / USD 39.15
Operating System Algorithms will walk you through in depth examples of algorithms that you would find in an operating system. Selected algorithms include process and disk scheduling.一起来看看 《Operating System Algorithms》 这本书的介绍吧!