转载自http://www.cnblogs.com/longgel/archive/2010/02/06/1664884.html

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

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

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

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

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

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

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


    <div>
    1.使用Action方法生成URL(Controller将是默认的)<br />
    <a href='<%= Url.Action("DemoAction") %>' title="">指定Action名称生成URL</a><br />
    <a href='<%= Url.Action("DemoAction","id") %>' title="">指定Action和一个RouteData(参数)生成URL</a><br />
    <a href='<%= Url.Action("DemoAction", new {id=2,category=5 })%>' title="">指定Action名称和多个参数生成URL</a><br />
    <a href='<%= Url.Action("DemoAction","DemoController")%>' title="">指定Action和Controller生成URL</a><br />
    <a href='<%= Url.Action("DemoAction","DemoController","id")%>' title="">指定Action,Controller和一个参数生成URL</a><br />
    <a href='<%= Url.Action("DemoAction","DemoController", new {id=2,category=5 })%>' title="">指定Action,Controller和多个参数生成URL</a><br />
    <a href='<%= Url.Action("DemoAction","DemoController", new {id=2,category=5 },"https")%>' title="">指定传输协议生成URL</a><br />
    <% 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><br /><br />
    2.使用Content方法将虚拟(相对)路径生成为绝对路径<br />
    <a href='<%= Url.Content("~/DemoController/DemoAction")%>' title="">指定虚拟路径生成绝对路径</a><br /><br />
    3.使用Encode加密URL<br />
    <a href='<%= Url.Encode("http://www.cnblogs.com/longgel/")%>' title="">加密过的URL连接</a><br /><br />
    4.使用RouteUrl生成URL<br />
    <a href='<%= Url.RouteUrl(tmp)%>' title="">指定RouteValue生成URL</a><br />
    <a href='<%= Url.RouteUrl("Default")%>' title="">指定RouteName生成URL</a><br />
    <a href='<%= Url.RouteUrl(rvd)%>' title="">指定多个参数生成URL</a><br />
    <a href='<%= Url.RouteUrl("Default",tmp) %>' title="">指定路由规则名和单个路由值</a><br />
    <a href='<%= Url.RouteUrl("Default",rvd) %>' title="">指定路由规则名和多个路由值</a><br />
    <a href='<%= Url.RouteUrl("Default",tmp,"https") %>' title="">指定传输协议</a><br />
    <a href='<%= Url.RouteUrl("Default",rvd,"https","www.cnblogs.com") %>' title="">指定主机名</a><br />        
    </div>

看看生成之后的html页面中的URL


    <div>
    1.使用Action方法生成URL(Controller将是默认的)<br />
    <a href='/simple/DemoAction' title="">指定Action名称生成URL</a><br />
    <a href='/id/DemoAction' title="">指定Action和一个RouteData(参数)生成URL</a><br />
    <a href='/simple/DemoAction?id=2&category=5' title="">指定Action名称和多个参数生成URL</a><br />
    <a href='/DemoController/DemoAction' title="">指定Action和Controller生成URL</a><br />
    <a href='/DemoController/DemoAction?Length=2' title="">指定Action,Controller和一个参数生成URL</a><br />
    <a href='/DemoController/DemoAction?id=2&category=5' title="">指定Action,Controller和多个参数生成URL</a><br />
    <a href='https://localhost/DemoController/DemoAction?id=2&category=5' title="">指定传输协议生成URL</a><br />
    
    <a href='https://local/DemoController/DemoAction?id=5&category=2' title="">指定主机名生成URL</a><br /><br />
    2.使用Content方法将虚拟(相对)路径生成为绝对路径<br />
    <a href='/DemoController/DemoAction' title="">指定虚拟路径生成绝对路径</a><br /><br />
    3.使用Encode加密URL<br />
    <a href='http%3a%2f%2fwww.cnblogs.com%2flonggel%2f' title="">加密过的URL连接</a><br /><br />
    4.使用RouteUrl生成URL<br />
    <a href='/simple/urlhelperdemo' title="">指定RouteValue生成URL</a><br />
    <a href='/Longgel/Index/Id' title="">指定RouteName生成URL</a><br />
    <a href='/simple/urlhelperdemo?id=5&category=2' title="">指定多个参数生成URL</a><br />/Longgel/Index/Id<br />
    <a href='/Longgel/Index/Id' title="">指定路由规则名和单个路由值</a><br />
    <a href='/Longgel/Index/Id?id=5&category=2' title="">指定路由规则名和多个路由值</a><br />
    <a href='https://localhost/Longgel/Index/Id' title="">指定传输协议</a><br />
    <a href='https://www.cnblogs.com/Longgel/Index/Id?id=5&category=2' title="">指定主机名</a><br />        
    </div>

(转)System.Web.Mvc.UrlHelper的学习与使用的更多相关文章

  1. System.Web.Mvc.UrlHelper的学习与使用

    我们学习一下UrlHelper帮助类,看类名也都知道这个类是用来帮我们生成URL在ASP.NET MVC应用程序中.让我们来看看该类给我们带来了哪些方便的方法和属性,UrlHelper提供了四个非常常 ...

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

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

  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. 数据结构C语言版干货------->线性表之顺序表

    一:头文件定义 /*************************************************************************** *项目 数据结构 *概要 逻辑 ...

  2. python中的单引号,双引号和三双引号的区别

    1.单引号和双引号没有区别.都可以用就是为了方便,减少写太多的转义字符. 比如: str='Let\'s go!' str="Let's go!" 两种等价,但是第二种写法显然更优 ...

  3. 查看字符串的编码chardet

    The Universal Character Encoding Detector chardet.detect("str") 返回:{‘confidence’:1.0,'enco ...

  4. Mysql备份脚本python编写

    #!/usr/bin/env python #-*- coding: UTF-8 -*- ####################################################### ...

  5. bzoj 2510: 弱题 概率期望dp+循环矩阵

    题目: Description 有M个球,一开始每个球均有一个初始标号,标号范围为1-N且为整数,标号为i的球有ai个,并保证Σai = M. 每次操作等概率取出一个球(即取出每个球的概率均为1/M) ...

  6. scrollspy.js--bug

    /** * 20140505 14.33 ycx * scrollspy.js中存在的bug!!!---为什么ui.tabs必须在scrollspy.js中的window.onload之前执行,也就是 ...

  7. Android ANR原因以及开发时如何预防

    经常可以在Android系统上发现ANR异常响应的问题.故了解一下ANR出现的原因 一. Android系统中,应用程序的响应是由ActivityManager 和 WindowManger系统服务监 ...

  8. WSGI 简介(使用python描述)

    WSGI 简介 背景 Python Web 开发中,服务端程序可以分为两个部分,一是服务器程序,二是应用程序.前者负责把客户端请求接收,整理,后者负责具体的逻辑处理.为了方便应用程序的开发,我们把常用 ...

  9. 服务器修改密码cmd

    net user  账号  要修改的密码

  10. route-显示并设置Linux内核中的网络路由表

    route命令 网络配置 route命令用来显示并设置Linux内核中的网络路由表,route命令设置的路由主要是静态路由.要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两 ...