上一次学习了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>

原文:http://www.cnblogs.com/longgel/archive/2010/02/06/1664884.html

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

  1. MVC的System.Web.Mvc.ViewPage小结

    Inherits="System.Web.Mvc.ViewPage<dynamic>这一句最好是自己手动修改,如果是维护用户数据,用户对象名是User,改成Inherits=&q ...

  2. (asp.net MVC学习)System.Web.Mvc.HtmlHelper学习及使用

    在ASP.NET MVC框架中没有了自己的控件,页面显示完全就回到了写html代码的年代.还好在asp.net mvc框架中也有自带的HtmlHelper和UrlHelper两个帮助类.另外在MvcC ...

  3. ASP.NET MVC2未能加载类型“System.Web.Mvc.ViewPage的解決方法

    问题描述: “/”应用程序中的服务器错误. 分析器错误 说明: 在分析向此请求提供服务所需资源时出错.请检查下列特定分析错误详细信息并适当地修改源文件. 分析器错误消息: 未能加载类型“System. ...

  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. <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

    Asp.net Mvc 未能加载类型“System.Web.Mvc.ViewPage 的解決方法 2010-11-30 17:31:51|  分类: .net mvc |举报 |字号 订阅   如果多 ...

  6. 修改System.Web.Mvc.WebViewPage创建自己的pageBase

    <system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, ...

  7. System.Web.Mvc 找到的程序集清单定义与程序集引用不匹配

    System.IO.FileLoadException: 未能加载文件或程序集"System.Web.Mvc, Version=5.0.0.0, Culture=neutral, Publi ...

  8. asp.net MVC3 “System.Web.Mvc.ModelClientValidationRule”问题

    错误提示: Error 1 The type 'System.Web.Mvc.ModelClientValidationRule' exists in both 'c:\Program Files ( ...

  9. MVC 5.0 之奇葩错误-<类型“ASP._Page__ViewStart_cshtml”不从“System.Web.WebPages.StartPage”继承>

    在实际项目中,我们通常添加MVC项目会先添加一个MVC Empty 的项目,然后需要什么在往里面添加. 但是Empty项目里面只有一个路由注册,而且没有_ViewStart.cshtml文件需要自己添 ...

随机推荐

  1. PS学习

    PS快捷键大全(转自UI中国PS教程) 摆脱鼠标流就靠这张图了!!!! 查看图像 使用导航器查看图像 选择窗口-->导航器 菜单命令 使用缩放工具查看图像 ctrl++ 以画布大小放大图像 ct ...

  2. JavaScript之firstChild属性、lastChild属性、nodeValue属性学习

    1.数组元素childNodes[0]有更直观易读的优点,这边在介绍一个有同样功能的属性,且更加语义化-------->firstChild属性 假设我们需要目标元素节点下的所有子元素中的第一个 ...

  3. 全球最快的JS模板引擎

    废话不多说,先上测试: 亲测请访问:[在线测试地址]单次结果不一定准确,请多测几次. tppl 的编译渲染速度是著名的 jQuery 作者 John Resig 开发的 tmpl 的 43 倍!与第二 ...

  4. jquery JS 左右方向键

    $(function (){ // $(document).keydown(function(e){ var code=e.which; switch (code) { case 38: //上 br ...

  5. su Authentication failure解决

    su Authentication failure解决 关于Ubuntu桌面系统su root时认证失败的问题 1. Ubuntu 默认没有给root用户设置密码,当我们su root命令时, 提示认 ...

  6. aJax学习之Ajax工作原理

    转自:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/18/2216553.html 在写这篇文章之前,曾经写过一篇关于AJAX技术的 ...

  7. Android 一个抽奖应用的逆向破解全流程之加固自己应用

    转自: <a href="http://www.pedant.cn/2014/07/22/crack-a-draw-app/">http://www.pedant.cn ...

  8. PHP学习(前言)

    PHP学习(前言) 都说做IT技术的都该写写博客,以前没写过,现在开始写写吧.不是给别人看,就当是自己的学习笔记了. 大三结束了,该找工作了,对web前端感兴趣,想从事前端工作,自然要会一门后台语言了 ...

  9. break在switch中的使用例子

    /* Name:break在switch中的使用例子 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月21日 03:16:52 Description:以 ...

  10. 一步一步Asp.Net MVC系列_权限管理总结(附MVC权限管理系统源码)

    在上一节中我们总结了关于权限控制的方式,我们这一节讲解关于权限控制中角色权限的授予处理等等并做本系列的总结. 首先,我们来谈谈权限控制中角色权限的控制,上一节只是针对权限拦截中比较粗的控制,如果我们需 ...