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

2
  • Does what I'm asking make sense? Commented Nov 2, 2010 at 19:12
  • yes. Take a look at these two questions: stackoverflow.com/questions/1613896/… and stackoverflow.com/questions/1038431/… 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. Commented Nov 2, 2010 at 19:24

1 Answer 1

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() %>
Sign up to request clarification or add additional context in comments.

5 Comments

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?
Where does this class go? Models or Controllers folder?
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: stackoverflow.com/questions/1226189/…
Awesome got that to work thanks. ASP.NET and C# are new to me so all this is pretty complex stuff.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.