1

I am using HTML.Raw in a few detail views that I have - so that the text output shows with any carriage returns or spaces. However, sometimes someone may type in a Web Address in the middle of text. I have no idea where the Web Address will be in the text. How can I get it so that the Web Address will show as a hyperlink?

The only things I can find in a search is where the coder knows the position of the Web Address. Here is an example (plus it is very old). ASP.NET MVC3 link with image inside @Html.Raw

FYI, this is a help desk system for people to open tickets for their problems. Sometimes they may put in that they cannot access http://nameofwebsite.com or something similar.

Here is my code:

@Html.Raw(Model.Note.Replace(Environment.NewLine, "<br/>"))
Community
  • 1
  • 1
djblois
  • 963
  • 1
  • 17
  • 52
  • There is no reason to use `@Html.Raw(Model.Note.Replace(Environment.NewLine, "
    "))` You can just style the element - `
    @Model.Note
    `
    –  Apr 06 '16 at 22:07

1 Answers1

0

You can use a regular expression when filling your model or even as a helper in the view to 'linkify' hyperlinks.

Here's an example of the regex (shamelessly stolen from this answer):

var html = Regex.Replace(html, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+" +
                     "\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?" +
                     "([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*$",
                     "<a href=\"$1\">$1</a>");

This will then render using Html.Raw() as a hyperlink.

Community
  • 1
  • 1
Gareth
  • 5,140
  • 5
  • 42
  • 73