ASP. NET MVC2 Chapter VIII II

Source: Internet
Author: User
§ 8. 3 Generating outgoing URLs

In the past, if you want to find an ID parameter from the detail. ASPX page

Myhyperlink. navigateurl = "~ /Details. aspx? Id = "+ Itemid;
But now we only need to write:
<A href = "/products/details/<%: viewdata [" Itemid "] %>"> More details </a>

 

§ 8. 3.1 generating hyperlinks with HTML. actionlink ()

The simplest way to generate a URL is to call HTML. actionlink () from a view template. For example:

 
<%: HTML. actionlink ("see all of our products", "list", "Products") %>

Its implementation effect is as follows:

<A href = "/products/List"> see all of our products </a>

Note: If you do not specify a controller, it will default to your current controller.

 

Passing extra parameters

You can pass the parameters required by the additional route entry.

 
<%: HTML. actionlink ("Red items", "list", "Products", new {color = "red", page = 2}, null) %>

Its implementation effect:

<A href = "/products/list? Color = Red & amp; page = 2 "> Red items </a>

If your route configuration adopts this configuration method products/LIST/{color}/{page}, the followingCode

 
<A href = "/products/LIST/red/2"> Red items </a>

 

How parameter defaults are handled

When we write the following code, and the default action of product is Index

<%: HTML. actionlink ("products Homepage", "Index", "Products") %>

Its implementation effect is as follows:

 
<A href = "/products"> Products homepage </a>

The index is omitted because it is the same by default.

 

Generating fully qualified absolute URLs

Html. actionlink () usually generates part of the URL (I. e./products, not the http://www.example.com/Products). If you want to generate a complete URL, as shown below:

<%: HTML. actionlink ("Click me", "myaction", "mycontroller", "HTTPS", "www.example.com", "anchorname", new {Param = "value "}, new {myattribute = "something"}) %>

Its effect is as follows:

 
<A myattribute = "something" href = "https://www.example.com/MyController/MyAction? Param = value # anchorname "> click me </a>
 
 
§ 8. 3.2 generating links and URLs from pure routing data

Html. routelink ()AndHtml. actionlink ()It is similar.

 
<%: HTML. routelink ("Click me", new {controller = "Products", Action = "list"}) %>

It will generate the following link

 
<A href = "/products/List"> click me </a>

Similarly,URL. routeurl ()AndURL. Action ()It is similar.

<%: URL. routeurl (New {controller = "Products", Action = "list"}) %>

Generate the following url:/products/List

Application in MVCProgramThese are not commonly used, but it is good to be familiar with them.

 

§ 8. 3.3 specify Ming redirections to generated URLs

To implement a redirection function, you only need to simply return the redirecttoaction () result and input the target controller and action method.

 
Public actionresult myactionmethod () {return redirecttoaction ("list", "Products ");}

If you want to obtain a URL string instead of HTTP redirection, you can do the following:

 
Public actionresult myactionmethod () {string url = URL. Action ("someaction", new {customerid = 456}); //... now do something with URL}

 

§ 8. 3.4 understanding the outbound URL-Matching Algorithm

§ 8. 3.5Generating hyperlinks with HTML. actionlink <t> and lambda expressions

§ 8. 3.6 working with named routes

 

 

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.