asp.net mvc routing extension: Linking and URL generation

Source: Internet
Author: User
Tags static class actionlink

The ASP.net routing system is designed to implement two "directional" paths, namely, the generation of routing and stack URLs for the incoming stack request by registering the routing table. The former is implemented by invoking the Getroutedata method of the RouteCollection object representing the global routing table, which relies on the RouteCollection Getvirtualpathdata method, Ultimately, it falls on the invocation of a method that inherits from the RouteBase routing object. For programming purposes, asp.net MVC designed the HtmlHelper and Urlhelper as two help classes, and we can call them by calling their Actionlink/routelink and action/ RouteUrl generates a link or URL based on a registered path. Essentially, the generation of URLs that Htmlhelper/urlhelper implements ultimately relies on the Getvirtualpathdata method described above.

First, Urlhelper V.s. HtmlHelper

Before introducing a link or URL through HtmlHelper and Urlhelper, let's take a look at their basic definitions. From the code snippet given below we can see that the Urlhelper object actually encapsulates a RouteCollection object that represents the RequestContext of the request context and the collection of routing objects. They correspond to read-only properties RequestContext and RouteCollection, and are initialized in constructors. If the RouteCollection object is not specified when constructing the Urlhelper, the table of the global path represented by the RouteTable static property routes is directly used.

   1:public class Urlhelper
2: {
3: //other Members
4: Public urlhelper (RequestContext requestcontext);
5: Public urlhelper (RequestContext requestcontext, routecollection routecollection);
6:
7: Public requestcontext RequestContext {get;}
8: Public routecollection routecollection {get;}
9:}

Take a look at the definition of HtmlHelper as shown below, which also has a routecollection attribute that represents a collection of routed objects. As with Urlhelper, if the constructor does not display the specified, the RouteCollection object that the RouteTable static property routes represents will be used to initialize the property.

   1:public class HtmlHelper
2: {
3: //other Members
4: Public htmlhelper (ViewContext viewcontext, Iviewdatacontainer viewdatacontainer);
5: Public htmlhelper (ViewContext viewcontext, Iviewdatacontainer Viewdatacontainer, routecollection RouteCollection);
6:
7: Public routecollection routecollection {get;}
8: Public viewcontext viewcontext {get;}
9:}
10:public class Viewcontext:controllercontext
11: {
A: //Omit members
13:}
14:public class ControllerContext
15: {
: //other Members
RequestContext RequestContext {get; set;}
: Public Virtual Routedata routedata {get; set;}
19:}

Because HtmlHelper is used only in view, it has a context for view that is represented by the ViewContext property. As for the corresponding type ViewContext of the property, it is a subclass of the controllercontext representing the controller context. The latter provides the current request context and routing data through the RequestContext and Routedata properties (in fact, the Routedata object represented by the Routedata property is already contained in the RequestContext object represented by the RequestContext property) 。

Second, Urlhelper.action V.s. Htmlhelper.actionlink

Urlhelper and HtmlHelper are used to generate a URL and link to an action for one controller, respectively, by action and ActionLink methods. The following code fragment lists all the action overloads for Urlhelper, and the parameters ActionName and controllername represent the name of the action and controller, respectively. The routevalues parameter, represented by an object or a routevaluedictionary type, represents a variable value that replaces a variable in a URL template. The parameters protocol and hostname represent transport protocols (such as HTTP and HTTPS) as full URLs, as well as host names.

 1:public class Urlhelper 
2: {
3://other member
4:public string Action (string actionname);
5:public string Action (String ActionName, Object routevalues);
6:public string Action (String actionname, string controllername);
7:public string Action (String actionname, RouteValueDictionary routevalues);
8:public string Action (String actionname, String controllername, Object routevalues);
9:public string Action (String actionname, String controllername, RouteValueDictionary routevalues);
Ten:
11:public string Action (String actionname, String controllername, Object Routevalues, String protocol) ;
12:public string Action (String actionname, String controllername, RouteValueDictionary routevalues, String prot Ocol, string hostName);
:}

For many action methods defined in Urlhelper, if we display a transport protocol (protocol parameter) or host name, a complete URL is returned, otherwise a relative URL is returned. If we do not specify the name of the controller (controllername parameter), then the name of the current controller is adopted. For Urlhelper, the current request context, represented by the RequestContext attribute, contains the corresponding routing information, which is the routedata represented by the Routedata attribute of RequestContext. The values attribute of Routedata must contain an element with a key of "controller", whose value represents the name of the current controller.

In System.Web.Mvc.Html.LinkExtensions, we define a series of ActionLink method overloads for HtmlHelper as shown below. As the name suggests, ActionLink no longer merely returns a URL, but rather generates a link (<a>...</a>), but the generated logic as the target URL is exactly the same as the urihelper.

1:public Static Class Linkextensions
2: {
3://other Members
4:public static mvchtmlstring ActionLink (This htmlhelper htmlhelper, string linkText, string actionname);
5:public static mvchtmlstring ActionLink (This htmlhelper htmlhelper, string linkText, String actionname, Object route Values);
6:public static mvchtmlstring ActionLink (This htmlhelper htmlhelper, string linkText, String actionname, String contr Ollername);
7:public static mvchtmlstring ActionLink (This htmlhelper htmlhelper, string linkText, String actionname, Routevaluedi Ctionary routevalues);
8:public static mvchtmlstring ActionLink (This htmlhelper htmlhelper, string linkText, String actionname, Object route Values, Object htmlattributes);
9:public static mvchtmlstring ActionLink (This htmlhelper htmlhelper, string linkText, String actionname, Routevaluedi Ctionary routevalues, idictionary<string, object> htmlattributes);
10:public static mvchtmlstring ActionLink (This htmlhelper htmlhelper, string linkText, String actionname, String cont Rollername, Object routevalues, Object htmlattributes);
11:public static mvchtmlstring ActionLink (This htmlhelper htmlhelper, string linkText, String actionname, String cont Rollername,routevaluedictionary routevalues, idictionary<string, object> htmlattributes);
12:public static mvchtmlstring ActionLink (This htmlhelper htmlhelper, string linkText, String actionname, String cont Rollername, string protocol, string HostName, String fragment, Object Routevalues, Object htmlattributes);
13:public static mvchtmlstring ActionLink (This htmlhelper htmlhelper, string linkText, String actionname, String cont Rollername, string protocol, string HostName, string fragment, RouteValueDictionary routevalues, idictionary<string, Object> htmlattributes);
14:}

Related Article

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.