How to Make Dynamic hyperlinks using C # in ASP. NET

Source: Internet
Author: User

We 've all seen blogs and other web sites that seem to be able to intersperse hyperlinks throughout their content, almost whimsically, if the given text is "linkable", or, formatted like a domain name or link of some kind.

I recently had the need to do this (for my site, nonsequiturs.com), and couldn't find a great source for a starting point, well, in C # anyway. so, I went ahead and made one using regular expressions and simple string replaces. it works well, and is relatively fast and reliable. I thought I 'd share the Code in case others had the need for this functionality.

Features:Automatically makes a hyperlink out of anything that appears to be a domain name or URL. will skip confusing text, like number sequences (e.g ., (10.35), and will ignore existing hyperlinks. you can enclose text with a special Tag if you want to specify an area that shoshould not be processed (see the code comments for more ). you can also add paramaters to the hyperlink tags that are generated, like"Target ="And so forth.

For more code samples, or to contact me directly, visit my site.

Collapse
 /// <Summary>  /// Convert domain names and URL paths to real web links.  /// Handles the most common web transport protocols. /// Existing <A> tag contents are ignored. Any valid URLs  /// Within a <nolink> </nolink> tag are ignored.  /// </Summary>  /// <Param name = "strvar"> string to process. </param>  /// <Param name = "Param"> string of parameters to insert  /// The resultant <A> tags, like target = "_ blank". </param>  /// <Returns> A string with Links </returns>  Public   Static   String Autohyperlinks ( String Strvar,String Param ){ // (C) 2006 Michael argentini, http://www.nonsequiturs.com.  //  // Please keep this copyright intact.  // You may use or modify this Code however you see fit,  // Within the scope of application or web site functionality.  // Distribution of this Code as an example or snippet is  // Prohibited. In this case, please link to the code example  // On the nonsequiturs.com site directly!  // First, process all <nolink> areas and Change Period // Characters temporarily to avoid auto-hyperlink processing.  String Final = strvar; RegEx = New RegEx (@ "<Nolink> (.*?) </Nolink>" , Regexoptions. ignorecase | regexoptions. singleline | regexoptions. cultureinvariant | regexoptions. Duplicate | regexoptions. Compiled); matchcollection thematches = RegEx. Matches (strvar ); For ( Int Index = 0 ; Index <thematches. Count; index ++) {final = final. Replace (thematches [Index]. tostring (), thematches [Index]. tostring (). Replace ( "." ,"[[PK: period]" ));} // Second, process all existing <A> tags and Change Period  // Characters in them temporarily to avoid auto-hyperlink processing. RegEx = New RegEx (@ "<(.*?) </A>" , Regexoptions. ignorecase | regexoptions. singleline | regexoptions. Fig | regexoptions. ignorepatternwhitespace | regexoptions. Compiled); thematches = RegEx. Matches (final ); For ( Int Index = 0 ; Index <thematches. Count; index ++) {final = final. Replace (thematches [Index]. tostring (), thematches [Index]. tostring (). Replace ("." , "[[PK: period]" ));} // Third, temporarily alter any digit Sequences  // That are formatted like domain names. Final = RegEx. Replace (final ,@ "(? <= \ D )\.(? = \ D )" , "[[PK: period]" ); // Fourth, look for, and process, any linkable domain names or URLs. RegEx tags = New RegEx (@ "([A-zA-Z0-9 \:/\-] * [a-zA-Z0-9 \-_] \" + @ ". [A-zA-Z0-9 \-_] [a-zA-Z0-9 \-_] [a-zA-Z0-9 \? \" + @"= _ \-/\.] * [^ <>,; \. \ S \) \ (\] \ [\"  "])" ); // Th, fix any inadvertently altered protocol strings. Final = tags. Replace (final, "<A href = \" HTTP: // $ & \ "" + Param + ">$ & </A>" ); Final = final. Replace ( "Http: // https ://" , "Https ://" ); Final = final. Replace ( "Http ://" , "Http ://" ); Final = final. Replace ( "Http: // ftp ://" ,"Ftp ://" ); Final = final. Replace ( "Http: // rtsp ://" , "Rtsp ://" ); Final = final. Replace ( "Http: // MMS ://" , "MMS ://" ); Final = final. Replace ( "Http: // pcast ://" , "Pcast ://" ); Final = final. Replace ( "Http: // SFTP ://" , "SFTP ://" ); Final = final. Replace ( "[[PK: period]" , "." ); Final = final. Replace ("<Nolink>" , "" ); Final = final. Replace ( "</Nolink>" , "" ); // Lastly, return the processed string.  Return Final ;}
 
 

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.