談談asp.net MVC中的AppendTrailingSlash以及LowercaseUrls ,你還記得嗎?

來源:互聯網
上載者:User

標籤:style   class   blog   code   java   http   

  asp.net MVC是一個具有極大擴充性的架構,可以在從Url請求開始直到最終的html的渲染之間進行擴充,所以要學好還是需要瞭解架構的運行原理,推薦Artech.

      今天我們回憶的不是MVC中的filter,也不是Controller的啟用或者是Action的執行,或者是Url路由RouteData的產生,我們來回憶的是RouteTable.Routes  ,即全域路由表的兩個屬性。AppendTrailingSlash以及LowercaseUrls。

     AppendTrailingSlash的作用就是是否在產生的Url末尾添加/ 斜線(如果沒有存在的話)。設定為true,則會在產生的Url末尾添加斜線,否則不會自動添加。

     LowercaseUrls的作用就是是否將產生的Url 轉換成小寫形式。因為對於搜尋引擎來說,可能Url地址的大小寫會不同對待。

這兩個屬性作用的是產生的Url,即會通過RouteTable.Routes.GetVirtualPath()通過傳入的請求上下文RequestContext來擷取Url。

可能你會說,如果我想實現產生的Url小寫,那麼我就將LowercaseUrls設定為true,就可以,事實真的是這樣的嗎?我們來做個實驗。

註冊的Url路由:

 

 1  public class RouteConfig 2     { 3         public static void RegisterRoutes(RouteCollection routes) 4         { 5             routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 6             routes.AppendTrailingSlash = true;//是否添加斜線 分隔字元 7             routes.LowercaseUrls = true;//Url是否小寫 8             routes.MapRoute( 9                       name: "Default",10                       url: "{controller}/{action}/{id}",11                       defaults: new { controller = "Home", action = "index", id = UrlParameter.Optional },12                       namespaces: new string[] { "MvcApplication3.Controllers" }13                   );18         }19     }

 

對應的Action方法:

1   [ActionName("List")]2         public ActionResult List()3         {4             ViewBag.employees = repository.EmployeeRepository.GetEmployees("");///是擷取一個幾個列表5             return View();6         }

對應的View:

 1 @{ 2     Layout = null; 3 } 4  5 <!DOCTYPE html> 6  7 <html> 8 <head> 9     <meta name="viewport" content="width=device-width" />10     <title>List</title>11 </head>12 <body>13     <div>14         @{15             List<MvcApplication3.Models.Employee> listEmployee = ViewBag.Employees as List<MvcApplication3.Models.Employee>;16         }17         @{18             if (listEmployee != null)19             {20                 <ul>21                     @{22 23                 foreach (var item in listEmployee)24                 {25                     <li>26                         @Html.RouteLink("RouteLink"+item.Name, "Default", new { action = "Detail", controller = "Home", id = item.Id })27                         <br />28                         @Html.ActionLink("ActionLink"+item.Name, "DETAIL", "home", new { id = item.Id }, new { target = "_blank" })                                                29                     </li>30                 }31                     }32                 </ul>33             }34             else35             {36 37                 <div>列表中無資料顯示</div>38             }39         }40     </div>41 </body>42 </html>

我們在視圖中通過Html.RouteLink以及Html.AtionLink來分別產生兩個超連結。執行的結果是什麼呢?

1  <li>2                         <a href="/home/detail/0/">RouteLinkguozhiqi0</a>  <!--這是通過RouteLink產生的Url-->3                         <br />4                         <a href="/home/DETAIL/0" target="_blank">ActionLinkguozhiqi0</a> <!--這是通過ActionLink產生的Url-->                                               5                     </li>

通過上面的結果我們可以看到一個奇怪的現象,就是通過Html.RouteLink()產生的Url應用了我們設定的屬性,即Url地址全域小寫,並且末尾有一個斜線分隔字元。

但是ActionLink卻沒有在末尾添加斜線,也沒有全部小寫。這究竟是為什麼呢?如果有人知道,麻煩告知一下具體區別。謝謝。

所以如果我們想產生全部小寫並且末尾有斜線分隔字元的Url地址,就採用RouteLink或者重寫Route的getVirtualpathData()來將所有大寫字母小寫化。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.