ASP.NET MVC- UrlHelper的用法

  UrlHelper提供了四个非常常用的四个方法

  1.Action方法通过提供Controller,Action和各种参数生成一个URL,

  2.Content方法是将一个虚拟的,相对的路径转换到应用程序的绝对路径,

  3.Encode方法是对URL地址进行加密,与Server.Encode方法一样。

  4.RouteUrl方法是提供在当前应用程序中规定的路由规则中匹配出URL。

  另外还有两个属性,分别是RequestContext和RouteCollection两个属性,分别指的是包含HTTP上下文和RouteData两个属性,另外,RouteCollection是整个当前应用程序中规定的路由规则。

  下面对上面的方法使用写成代码看

  

        1.Url.Action的使用
        <a href='@Url.Action("Test")'>这是一个测试</a>
        <a href='@Url.Action("Test", "Home")'>这是一个测试</a>
        <a href='@Url.Action("Test", "Home", new { xId = 2, cat = 5 })'>这是一个测试</a>
        <a href='@Url.Action("Test", "Home", new { xid = 2, cat = 5 }, "https")'>这是一个测试</a>
        生成的代码
        <a href='/Home/Test'>这是一个测试</a>
        <a href='/Home/Test'>这是一个测试</a>
        <a href='/Home/Test?xId=2&amp;cat=5'>这是一个测试</a>
        <a href='https://localhost/Home/Test?xid=2&amp;cat=5'>这是一个测试</a>

        2.使用Content方法将虚拟(相对)路径生成为绝对路径
        <a href='@Url.Content("~/DemoController/DemoAction")' title="">指定虚拟路径生成绝对路径</a><br /><br />
        3.使用Encode加密URL
        <a href='@Url.Encode("http://www.cnblogs.com/")' title="">加密过的URL连接</a>

  转载自:http://www.cnblogs.com/sunmoonstarash/articles/1775842.html

原文地址:https://www.cnblogs.com/cxeye/p/4985242.html