《ASP.NET MVC4 WEB编程》学习笔记------UrlHelper
HtmlHelper帮助我们生成Html标记代码;UrlHelper帮助我们生成URL链接地址
我们学习一下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是整个当前应用程序中规定的路由规则。
下面对上面的方法使用写成代码看
<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>
最新文章
- 利用zlib库进行zip解压
- HDU 5253 最小生成树(kruskal)+ 并查集
- 【PHP】金额数字转换成大写形式
- poll实现
- get_magic_quotes_gpc() 内置函数
- C++中的常对象和常对象成员
- 【图像处理】Bilinear Image Scaling
- Java课程设计 - 学生基本信息管理
- DevOps之归纳总结
- bzoj 4501 旅行
- IIS+nginx反向代理 负载均衡
- python--ModuleFoundError
- SQL Server实际执行计划COST";欺骗";案例
- Windbg 内核态调试用户态程序然后下断点正确触发方法(亲自实现发现有效)
- [C++]Linux之C编程异常[true未定义解决方案]
- CT ubuntu 16.04安装 adobe flash player
- vue父子组件嵌套的时候遇到 - Component template should contain exactly one root element. If you are using v-i
- DOM中的事件对象和IE事件对象
- python requests 简单实现易班登录,自动点赞,评论,发表
- iOSUITableView头部带有图片并且下拉图片放大效果
热门文章
- Ibatis学习总结2--SQL Map XML 配置文件
- Servlet Study 1
- 【BZOJ 3674】可持久化并查集加强版&;【BZOJ 3673】可持久化并查集 by zky 用可持久化线段树破之
- Quartz 定时任务管理
- Java设计模式-状态模式(State)
- Java设计模式-责任链模式(Chain of Responsibility)
- 【CodeForces 613A】Peter and Snow Blower
- POJ1947 Rebuilding Roads
- TYVJ1000 A+B problem [存个高精模板]
- iconv命令详解