继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Asp.net实现页面伪静态

ITMISS
关注TA
已关注
手记 213
粉丝 51
获赞 244

摘要

从页面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的物理文件在服务器上是不存在的。

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP