0

I have the following function inside my HomeController class:

public class HomeController : Controller
    {

        public string Strip(string text)
        {
            return Regex.Replace(text,@"<(.|\n)*?>",string.Empty);
        }

On my view I have the following to show an article from a database:

<%= item.story %>

A typical article will look like the following:

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea <em>commodo consequat</em>.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

As you can see the text has HTML tags throughout. What I would like to do is use my Strip function with item.story to remove those HTML tags. After that I would like to truncate the remaining text into 20 WORDS.

So I'll end up with something along the lines of:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua dolore... with no HTML tags and only about 20 WORDS long.

How do I do this with my current code? Is the HomeController the correct place for the Strip function to be? Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
Cameron
  • 27,963
  • 100
  • 281
  • 483
  • Does what I'm asking make sense? – Cameron Nov 02 '10 at 19:12
  • yes. Take a look at these two questions: http://stackoverflow.com/questions/1613896/truncate-string-on-whole-words-in-net-c and http://stackoverflow.com/questions/1038431/how-to-clean-html-tags-using-c Also, I wouldn't keep the methods in the HomeController, you could make a method on the Item class to return the cleaned version of Story, or make an extension method. – Brandon Nov 02 '10 at 19:24

1 Answers1

5

Controllers should hold Actions. What you're looking for is probably an extension method, one that you can call on your string.

You'll probably need two extensions, one to strip out HTML tags, the other to create the 20-word-short-format version of your paragraph.

UPDATE To answer your question...

You can create a new class (say ParagraphExtension.cs) and put your string extensions in this class:

namespace myApp.Util.Extensions
{
      public static class ParagraphExtension
      {
           public static string RemoveHTMLTags(this string content)
           {
                   // insert code
           }

           public static string ShortFormParagraph(this string content)
           {
                   // insert code
           }
       }
}

In your view, you can then import the namespace in which this class is found:

<%@ Import Namespace="myApp.Util.Extensions" %>

Finally, you can call the extensions from within the view:

<%= item.story.RemoveHTMLTags().ShortFormParagraph() %>
Remus
  • 1,433
  • 3
  • 14
  • 24
  • Okies thanks. Two more questions. 1) How do I create this extension method using the function I have. 2.) How do I call this on my string `item.story`? – Cameron Nov 02 '10 at 19:28
  • Where does this class go? Models or Controllers folder? – Cameron Nov 02 '10 at 19:42
  • 1
    I usually put mine outside of Models and Controllers. I'll have a namespace like Util or Common that I'd put these type of files in. Here's a discussion on that topic: http://stackoverflow.com/questions/1226189/best-practices-c-extension-methods-namespace-and-promoting-extension-methods – Remus Nov 02 '10 at 19:46
  • Awesome got that to work thanks. ASP.NET and C# are new to me so all this is pretty complex stuff. – Cameron Nov 02 '10 at 20:27
  • Its probably worth pointing out here that Extention methods are static methods in a static class that has the keyword 'this' in front of its first parameter. This makes the method usable as though it was directly on objects of the type of the first parameter. It does not however have access to private of internal fields of the object type it is extending – stevenrcfox Jan 03 '12 at 16:38