Moving from classic ASP to dotnet (C#) - I have a function (method) that
searches a string and replaces instances of urls and email addresses with
hyperlinks. The method works as I want it to. Currently it resides in the
code-behind for the pages I want it to do its thing on. Note, it is used on
more than one page.
In classic ASP, I would have placed this method as a VBScript function in an
include file and called it for each page I wanted to use it, thus
restricting me to one copy of the function (promoting easier maintenance).
This is my first exposure to OOP, so I'm a bit hazy on the best way to
achieve something similar in dotnet. Do I create a class with just the one
method and stick it in the App_Code folder? How do I then get it to work on
a page?
Also, I'm keen to understand exactly what it is in its current context. Am
I right in believing that I have, by virtue of adding a page called
jobDetails.aspx, created a class called jobDetails, which is based on the
Page class? And if this is correct, have I simply added a new method
(ReplaceLinks) to this class?
Here's the code_behind (omitting the "using" statements):
public partial class jobDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string ReplaceLinks(string InputText)
{
Regex urlregex = new Regex(@"(^|[\n ])(?<url>(www|ftp)\.[^
,""\s<]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
InputText = urlregex.Replace(InputText, "<a href=\"http://${url}\"
target=\"_blank\">${url}</a>");
Regex httpurlregex = new Regex(@"(^|[\n ])(?<url>(http://www)\.[^
,""\s<]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
InputText = httpurlregex.Replace(InputText, "<a href=\"${url}\"
target=\"_blank\">${url}</a>");
Regex emailregex = new
Regex(@"(?<url>[a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
InputText = emailregex.Replace(InputText, "<a
href=\"mailto:${url}\">${url}</a>");
return InputText.Replace("\r", "<br />");
}
}
Thanks
Mike