mvc路由配置.html結尾的偽靜態


    mvc 標准的寫法 通常是(http://localhost:8149/Home/Index) 路由配置如下:

    有時候需求 如 http://localhost:8149/Home/Index 改為http://localhost:8149/index.html 讓其看起來更加像一個靜態網站

   

       //配置首頁 偽靜態 路由
            routes.MapRoute("pc_index", "index.html", new { controller = "Home", action = "Index" });

 然而  

 

解決方式一(不建議)修改 Web.config 文件  

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" >
</modules>
</system.webServer>

這種方式強烈不建議:
1、這些問題的形式是使所有注冊的HTTP模塊在每個請求上運行,而不僅僅是托管請求(例如.html)。 這意味着模塊將永遠運行.jpg .gif .css .aspx等
2、浪費資源
3、並具有可能導致錯誤的全局效應

更好的解決方案(方式二)

<system.webServer>
    <modules > 
      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
    </modules>
  </system.webServer>

 

更好的解決方案(方式三)

 <system.webServer> 
    <handlers>
      <add  name="htmlHandler" verb="GET,HEAD" path="*.html" type="System.Web.StaticFileHandler"/>
    </handlers>
  </system.webServer>

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM