0

I'd like to add some kind of simple URL resolution and formatting to my C# and jQuery-based ASP.NET web application. I currently allow users to add simple text-based descriptions to items and leave simple comments ('simple' as in I only allow plain text).

What I need to support is the ability for a user to enter something like:

Check out this cool link: http://www.really-cool-site.com

...and have the URL above properly resolved as a link and automagically turned into a clickable link...kinda like the way the editor in StackOverflow works. Except that we don't want to support BBCode or any of its variants. The user experience would actually be more like the way Facebook resolves user-generated URL's.

What are some jQuery + C# solutions I should consider?

Armchair Bronco
  • 2,367
  • 4
  • 31
  • 44
  • 6
    [Linkify?](http://webpageauthority.wordpress.com/2010/08/10/linkify-jquery-plugin-that-automatically-finds-urls-in-text-content-and-changes-them-into-proper-hyperlinks/) – bzlm Aug 18 '11 at 07:37
  • 3
    @bzlm please make this an answer! ... –  Aug 18 '11 at 07:38
  • possible duplicate of [C# code to linkify urls in a string](http://stackoverflow.com/questions/758135/c-code-to-linkify-urls-in-a-string) –  Aug 18 '11 at 07:40
  • Or [jQuery Text to Link Script?](http://stackoverflow.com/questions/247479/jquery-text-to-link-script) – Spycho Aug 18 '11 at 07:49
  • Reviewing all the referenced links from above. Thanks in advance. – Armchair Bronco Aug 18 '11 at 08:01
  • @Andreas, too much risk of duplicate-question for that. :) – bzlm Aug 18 '11 at 11:20

2 Answers2

0

There's another question with a solution that might help you. It uses a regex in pure JS.

Personally though, I would do it server-side when the user submits it. That way, you only need to do it once, rather than every time you display that text. You could use a similar regex in C#.

Community
  • 1
  • 1
Spycho
  • 7,698
  • 3
  • 34
  • 55
  • Lots of good information in the linked question. Digging through it now. I always assumed I'd need a solution that used a combination of jQuery + some kind of managed code DLL on the server to really clean up the URL. Aren't the client-side only solutions vulnerable to hacks? – Armchair Bronco Aug 18 '11 at 08:01
  • If all it does is turn text links into anchor tag links, all that can be "hacked" is that users would be able to post links to anything they like, and then those links would (presumably?) be viewable by other users. It wouldn't open up any security vulnerabilities on your server, as the server-client interface won't have changed. Users can, of course, fiddle around with the script that runs on the page, but this will only affect them, not other users. – Spycho Aug 18 '11 at 08:06
0

I ended up using server-side C# code to do the linkification. I use an AJAX-jQuery wrapper to call into a PageMethod that does the work.

The PageMethod both linkifies and sanitizes the user-supplied string, then returns the result.

I use the Microsoft Anti-Cross Site Scripting Library (AntiXSS) to sanitize:

http://www.microsoft.com/download/en/details.aspx?id=5242

And I use C# code I found here and there to resolve and shorten links using good olde string parsing and regular expressions.

My method is not as cool as the way FaceBook does it in real time, but at least now my users can add links to their descriptions and comments.

Armchair Bronco
  • 2,367
  • 4
  • 31
  • 44