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

  • 相关阅读:
    今天学到的新知识自己的电脑可以像Github Pages、码云 Pages一样发布静态资源
    移动端安卓开发学习记录Android Studio打断点调试操作步骤记录
    图文并茂学习记录从零开始进行微信小程序开发+引入Vant Weapp组件
    代码小DEMO随笔JS原生手机版本alert弹框
    移动端安卓开发学习记录Android Studio使用adb链接夜神模拟器常用指令
    图文并茂的学习笔记微信小程序自定义tabbar
    解决微信小程序 自定义tabBar 切换时候闪烁问题
    App扫码登录
    TypeScript 参数简化实战(进阶知识点conditional types) 广东靓仔
    前端进阶指南(优秀文章) 广东靓仔
  • 原文地址:https://www.cnblogs.com/wolf-sun/p/6728253.html
Copyright © 2020-2023  润新知