MVC中的UrlHelper

authour: chenboyi
updatetime: 2015-04-27 22:32:47
friendly link:  

 

 

 


 1,CodeSimple:

ps:因为UrlHelper涉及的知识点比较少,直接上源码演示

相比于传统的<a herf="/myController/Index">跳转</a>这种写法,

<a herf="@Url.Action("Index","myController")">跳转</a>有一个小优势,就是它是匹配路由规则的;

Url.Encode();对url进行转码,返回string。

 1 <!-- urlhelper.cshtml  --> 

 2 <div>

 3         <hr />

 4  @*Url.Action():根据指定的参数,根据路由规则中 url占位符生成对于的 url  重点 *@  5  @Url.Action("Add", new { controller = "UrlHelperDemo", id = 100 })

 6         <br />

 7         Url.Encode=  @Url.Encode("http://www.baidu.com/新闻/a.aspx?id=八戒")

 8         <br />

 9         @{

10             var s = Server.UrlDecode(Url.Encode("http://www.baidu.com/新闻/a.aspx?id=八戒"));

11  @s

12         }

13         <br />

14  @*指定某条路由规则来生成相应的url  重点*@ 15  @Url.RouteUrl("Default1", new { controller = "Home", action = "index", id = 100,name="八戒" })

16         <br />        

17         <a href="@Url.Action("Add", new { controller = "UrlHelperDemo", id = 100 })">跳转</a>

18         <br />

19         <a href="@Url.RouteUrl("Default1", new { controller = "Home", action = "index", id = 100,name="八戒" })">跳转</a>

20     </div>

 

你可能感兴趣的:(help)