Go Study and use of System.Web.Mvc.UrlHelper

Source: Internet
Author: User

Reprinted from Http://www.cnblogs.com/longgel/archive/2010/02/06/1664884.html

The last time I learned the HtmlHelper help class, this time we learn about the Urlhelper help class, and see the class name also know that this class is used to help us generate URLs in an ASP. NET MVC application. Let's look at some of the convenient methods and properties that this class brings to us, Urlhelper provides four very common four methods,

The 1.Action method generates a URL by providing controller,action and various parameters,

The 2.Content method is to convert a virtual, relative path to the absolute path of the application,

The 3.Encode method is to encrypt the URL address, just like the Server.encode method.

The 4.RouteUrl method is to provide a URL that matches the routing rules specified in the current application.

There are also two properties, the RequestContext and routecollection two attributes, respectively, that contain the HTTP context and routedata two properties, plus, RouteCollection is the routing rule specified throughout the current application.

The following method is used as code to see

    <Div>
1. Use the action method to generate the URL (the controller will be the default)<BR/>
<ahref= ' <%=url.action ("demoaction")%>' title= ' > Specify the action name to generate the URL</a><BR/>
<ahref= ' <%=url.action ("Demoaction", "id")%>' title= ' > Specify an action and a routedata (parameter) to generate the URL</a><BR/>
<ahref= ' <%=url.action ("Demoaction", new {ID=2,category=5})%>' title= ' > Specify the action name and multiple parameters to generate the URL</a><BR/>
<ahref= ' <%=url.action ("Demoaction", "Democontroller")%>' title= ' > Specify the action and controller to generate the URL</a><BR/>
<ahref= ' <%=url.action ("Demoaction", "Democontroller", "id")%>' title= ' > Specify Action,controller and a parameter to generate the URL</a><BR/>
<ahref= ' <%=url.action ("Demoaction", "Democontroller", new {ID=2,category=5})%>' title= ' > Specify Action,controller and multiple parameters to generate the URL</a><BR/>
<ahref= ' <%=url.action ("Demoaction", "Democontroller", new {ID=2,category=5}, "https")%>' title= ' > Specify the transport protocol to generate the URL</a><BR/>
<%var rvd= Newroutevaluedictionary ();
RvD. ADD ("ID", 5);
RvD. ADD ("category", 2);
var tmp= 5; %>
<ahref= ' <%=url.action ("Demoaction", "Democontroller", RVD, "https", "local")%>' title= ' > Specify host name to generate URL</a><BR/><BR/>
2. Use the content method to generate a virtual (relative) path as an absolute path<BR/>
<ahref= ' <%=url.content ("~/democontroller/demoaction")%>' title= ' > Specify the virtual path to generate an absolute path</a><BR/><BR/>
3. Encrypt URLs using encode<BR/>
<ahref= ' <%=Url.encode ("http://www.cnblogs.com/longgel/")%>' title= ' > Encrypted URL connection</a><BR/><BR/>
4. Using RouteUrl to generate URLs<BR/>
<ahref= ' <%=url.routeurl (TMP)%>' title= ' > Specify routevalue to generate the URL</a><BR/>
<ahref= ' <%=Url.routeurl ("Default")%>' title= ' > Specify routename to generate the URL</a><BR/>
<ahref= ' <%=url.routeurl (RVD)%>' title= ' > Specify multiple parameters to generate the URL</a><BR/>
<ahref= ' <%=Url.routeurl ("Default", tmp)%>' title= ' > Specify a routing rule name and a single route value</a><BR/>
<ahref= ' <%=Url.routeurl ("Default", RVD)%>' title= ' > Specify the routing rule name and multiple route values</a><BR/>
<ahref= ' <%=Url.routeurl ("Default", tmp, "https")%>' title= ' > Specify the Transport protocol</a><BR/>
<ahref= ' <%=Url.routeurl ("Default", RVD, "https", "www.cnblogs.com")%>' title= ' > Specify host name</a><BR/>
</Div>

Look at the URLs in the HTML page after the build

    <Div>
1. Use the action method to generate the URL (the controller will be the default)<BR/>
<ahref= '/simple/demoaction 'title="">Specify the action name to generate the URL</a><BR/>
<ahref= '/id/demoaction 'title="">specify an action and a routedata (parameter) to generate a URL</a><BR/>
<ahref= '/simple/demoaction?id=2&category=5 'title="">Specify the action name and multiple parameters to generate the URL</a><BR/>
<ahref= '/democontroller/demoaction 'title="">Specify action and controller build URLs</a><BR/>
<ahref= '/democontroller/demoaction? Length=2 'title="">specify Action,controller and a parameter to generate a URL</a><BR/>
<ahref= '/democontroller/demoaction?id=2&category=5 'title="">specifying Action,controller and multiple parameters to generate URLs</a><BR/>
<ahref= ' https://localhost/DemoController/DemoAction?id=2&category=5 'title="">Specify the transport protocol generation URL</a><BR/>

<ahref= ' https://local/DemoController/DemoAction?id=5&category=2 'title="">Specify host name to generate URL</a><BR/><BR/>
2. Use the content method to generate a virtual (relative) path as an absolute path<BR/>
<ahref= '/democontroller/demoaction 'title="">Specify the virtual path to generate an absolute path</a><BR/><BR/>
3. Encrypt URLs using encode<BR/>
<ahref= ' http%3a%2f%2fwww.cnblogs.com%2flonggel%2f 'title="">Encrypted URL connection</a><BR/><BR/>
4. Using RouteUrl to generate URLs<BR/>
<ahref= '/simple/urlhelperdemo 'title="">specify routevalue to generate URLs</a><BR/>
<ahref= '/longgel/index/id 'title="">specify routename to generate URLs</a><BR/>
<ahref= '/simple/urlhelperdemo?id=5&category=2 'title="">specifying multiple parameters to generate URLs</a><BR/>/longgel/index/id<BR/>
<ahref= '/longgel/index/id 'title="">Specify a routing rule name and a single route value</a><BR/>
<ahref= '/longgel/index/id?id=5&category=2 'title="">specifying the routing rule name and multiple route values</a><BR/>
<ahref= ' Https://localhost/Longgel/Index/Id 'title="">Specify the transport protocol</a><BR/>
<ahref= ' https://www.cnblogs.com/Longgel/Index/Id?id=5&category=2 'title="">Specify host name</a><BR/>
</Div>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.