我们学习一下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的学习与使用的更多相关文章

  1. (asp.net MVC学习)System.Web.Mvc.UrlHelper的学习与使用

    上一次学习了HtmlHelper帮助类,这次我们学习一下UrlHelper帮 助类,看类名也都知道这个类是用来帮我们生成URL在ASP.NET MVC应用程序中.让我们来看看该类给我们带来了哪些方便的 ...

  2. (转)System.Web.Mvc.UrlHelper的学习与使用

    转载自http://www.cnblogs.com/longgel/archive/2010/02/06/1664884.html 上一次学习了HtmlHelper帮助类,这次我们学习一下UrlHel ...

  3. System.Web.Mvc.Controller.cs

    ylbtech-System.Web.Mvc.Controller.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicK ...

  4. Could not load type 'System.Web.Mvc.ViewPage<dynamic>' in asp.net mvc2 after publishing the website

    在WebConfig里 找到 <pages></pages> <pages pageParserFilterType="System.Web.Mvc.ViewT ...

  5. MVC-命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Html”(是否缺少程序集引用?)

    如上截图,明明引用了“System.web.mvc”,可是还出这样的错误. 解决方法: 1.右键引用的“System.Web.Mvc” 2.<复制本地>一样选择<True> 3 ...

  6. System.Web.Mvc.dll在各个版本MVC中的文件位置

    the default folder would be like the following: MVC 5 C:\Program Files (x86)\Microsoft ASP.NET\ASP.N ...

  7. CS0234: 命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Html、Ajax”(是否缺少程序集引用?)

    从SVN上down下来的程序,编译报了一大堆的错,发现是缺少引用,但是明明引用了,后来打开引用,发现system.web.mvc这个引用打着叹号,如图: 后来重新引用了本机的system.web.mv ...

  8. vs2012运行项目报未能加载文件或程序集“System.Web.Mvc, Version=4.0.0.1,Culture=neutral”问题和解决方法

    原先本地项目版本(4.0.0.1)高于服务器版本(4.0.0.0),本地项目改成服务器版本4.0.0.0时,发布后的项目报这个错误

  9. ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”

    ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”,这在给定的上下文中无效 这是一个与Controller.F ...

随机推荐

  1. redis实现tomcat集群session共享

    1.部署两个tomcat节点,使用nginx实现集群(见http://www.cnblogs.com/zhangzhi0556/articles/nginx.html):   2.redis安装(见h ...

  2. OpenGL ES crash notes 01 - Nice to meet you

    这篇笔记完全参照<OpenGL.ES.3.0.Programming.Guide.2nd.Edition>,摘出部分内容只为学习参考. 为什么要用英文:无论是D3D的SDK还是OES的Sp ...

  3. mysql workbench

    下载地址:http://dev.mysql.com/downloads/ 详情:http://baike.baidu.com/link?url=sWV3b2pWdr8cvCxEZYrB9CzLD9Bl ...

  4. linux下驱动webcam

    linux自带驱动只支持一些型号的camera,具体型号见http://www.ideasonboard.org/uvc/ 所以有些购买的webcam不能够在linux中被点亮,而且有些厂家只为了在W ...

  5. UpdatePanel 中 导出Excel按钮

    UpdatePanel 中 导出Excel按钮 要加 Triggers </ContentTemplate> <Triggers> <asp:PostBackTrigge ...

  6. DNX SDK版本 “dnx-clr-win-x86.1.0.0-beta5”无法安装

    打开VS2015,出现 “DNX SDK版本 “dnx-clr-win-x86.1.0.0-beta5”无法安装,该解决方案将对此会话使用DNX SDK版本“dnx-clr-win-x86.1.0.0 ...

  7. Python3利用BeautifulSoup4批量抓取站点图片的代码

    边学边写代码,记录下来.这段代码用于批量抓取主站下所有子网页中符合特定尺寸要求的的图片文件,支持中断. 原理很简单:使用BeautifulSoup4分析网页,获取网页<a/>和<im ...

  8. Linux 中 Vi 编辑器的简单操作

    Linux 中 Vi 编辑器的简单操作 Vi 编辑器一共有3种模式:命名模式(默认),尾行模式,编辑模式.3种模式彼此需要切换. 一.进入 Vi 编辑器的的命令 vi  filename //打开或新 ...

  9. nginx下开启pathinfo模式

    第一种方式是通过重写url来实现pathinfo模式: location / { if (!-e $request_filename){ rewrite ^/(.*)$ /index.php?s=/$ ...

  10. 深入理解display属性

    display 属性在网页布局中很常见,但是之前一直不了解他的各个属性的区别,只是简单的使用block.none属性,对于其他属性都不清楚,今天详细的学习和实践display属性的各个方面 定义 di ...