System.Web.Mvc.UrlHelper的學習與使用

來源:互聯網
上載者:User

標籤:

我們學習一下UrlHelper協助類,看類名也都知道這個類是用來幫我們產生URL在ASP.NET MVC應用程式中。讓我們來看看該類給我們帶來了哪些方便的方法和屬性,UrlHelper提供了四個非常常用的四個方法,

1.Action方法通過提供Controller,Action和各種參數產生一個URL,

2.Content方法是將一個虛擬,相對的路徑轉換到應用程式的絕對路徑,

3.Encode方法是對URL地址進行加密,與Server.Encode方法一樣。

4.方法是提供在當前應用程式中規定的路由規則中匹配出URL。

另外還有兩個屬性,分別是RequestContext和RouteCollection兩個屬性,分別指的是包含HTTP上下文和RouteData兩個屬性,另外,RouteCollection是整個當前應用程式中規定的路由規則。

下面對上面的方法使用寫成代碼看

 

    1.使用Action方法產生URL(Controller將是預設的)

    <a href=‘<%= Url.Action("DemoAction") %>‘ title="">指定Action名稱產生URL</a>

    <a href=‘<%= Url.Action("DemoAction","id") %>‘ title="">指定Action和一個RouteData(參數)產生URL</a>

    <a href=‘<%= Url.Action("DemoAction", new {id=2,category=5 })%>‘ title="">指定Action名稱和多個參數產生URL</a>

    <a href=‘<%= Url.Action("DemoAction","DemoController")%>‘ title="">指定Action和Controller產生URL</a>

    <a href=‘<%= Url.Action("DemoAction","DemoController","id")%>‘ title="">指定Action,Controller和一個參數產生URL</a>

    <a href=‘<%= Url.Action("DemoAction","DemoController", new {id=2,category=5 })%>‘ title="">指定Action,Controller和多個參數產生URL</a>

    <a href=‘<%= Url.Action("DemoAction","DemoController", new {id=2,category=5 },"https")%>‘ title="">指定傳輸協議產生URL</a>

    <% var rvd = new RouteValueDictionary();

       rvd.Add("id", 5);

       rvd.Add("category", 2);

       var tmp = 5;  %>

    <a href=‘<%= Url.Action("DemoAction","DemoController", rvd,"https","local")%>‘ title="">指定主機名稱產生URL</a>  
         產生的結果:       

   <a href=‘/simple/DemoAction‘ title="">指定Action名稱產生URL</a>

    <a href=‘/id/DemoAction‘ title="">指定Action和一個RouteData(參數)產生URL</a>

    <a href=‘/simple/DemoAction?id=2&category=5‘ title="">指定Action名稱和多個參數產生URL</a>

    <a href=‘/DemoController/DemoAction‘ title="">指定Action和Controller產生URL</a>

    <a href=‘/DemoController/DemoAction?Length=2‘ title="">指定Action,Controller和一個參數產生URL</a>

    <a href=‘/DemoController/DemoAction?id=2&category=5‘ title="">指定Action,Controller和多個參數產生URL</a>

    <a href=‘https://localhost/DemoController/DemoAction?id=2&category=5‘ title="">指定傳輸協議產生URL</a>

    <a href=‘https://local/DemoController/DemoAction?id=5&category=2‘ title="">指定主機名稱產生URL</a>

    2.使用Content方法將虛擬(相對)路徑產生為絕對路徑

    <a href=‘<%= Url.Content("~/DemoController/DemoAction")%>‘ title="">指定虛擬路徑產生絕對路徑</a>

<a href=‘/DemoController/DemoAction‘ title="">指定虛擬路徑產生絕對路徑</a>

    3.使用Encode加密URL

    <a href=‘<%= Url.Encode("http://www.cnblogs.com/longgel/")%>‘ title="">加密過的URL串連</a>  
         產生的結果:  

    <a href=‘http%3a%2f%2fwww.cnblogs.com%2flonggel%2f‘ title="">加密過的URL串連</a> 

    4.使用RouteUrl產生URL

    <a href=‘<%= Url.RouteUrl(tmp)%>‘ title="">指定RouteValue產生URL</a>

    <a href=‘<%= Url.RouteUrl("Default")%>‘ title="">指定RouteName產生URL</a>

    <a href=‘<%= Url.RouteUrl(rvd)%>‘ title="">指定多個參數產生URL</a>

    <a href=‘<%= Url.RouteUrl("Default",tmp) %>‘ title="">指定路由規則名和單個路由值</a>

    <a href=‘<%= Url.RouteUrl("Default",rvd) %>‘ title="">指定路由規則名和多個路由值</a>

    <a href=‘<%= Url.RouteUrl("Default",tmp,"https") %>‘ title="">指定傳輸協議</a>

    <a href=‘<%= Url.RouteUrl("Default",rvd,"https","www.cnblogs.com") %>‘ title="">指定主機名稱</a>  
         產生的結果:         

    <a href=‘/simple/urlhelperdemo‘ title="">指定RouteValue產生URL</a>

    <a href=‘/Longgel/Index/Id‘ title="">指定RouteName產生URL</a>

    <a href=‘/simple/urlhelperdemo?id=5&category=2‘ title="">指定多個參數產生URL</a><br />/Longgel/Index/Id

    <a href=‘/Longgel/Index/Id‘ title="">指定路由規則名和單個路由值</a>

    <a href=‘/Longgel/Index/Id?id=5&category=2‘ title="">指定路由規則名和多個路由值</a>

    <a href=‘https://localhost/Longgel/Index/Id‘ title="">指定傳輸協議</a>

    <a href=‘https://www.cnblogs.com/Longgel/Index/Id?id=5&category=2‘ title="">指定主機名稱</a>

System.Web.Mvc.UrlHelper的學習與使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.